android - 如何动态添加单选按钮而不丢失 View 内容

标签 android radio-group

我的布局是这样的

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:textStyle="bold" 
            android:text="Please select the restaurant you want to rate: " />

        <RadioGroup android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/SelectRestaurant">

         </RadioGroup>
         <Button android:layout_width="100px"
         android:layout_height="wrap_content" 
         android:text="Rate it" 
         android:id="@+id/submitSelection"/>
</LinearLayout>

当我在 java 文件中将单选按钮添加到单选按钮时,单选按钮相互重叠,提交按钮消失了。我想单选按钮会覆盖其他 View 元素。如何在不丢失其他元素的情况下动态添加这些单选按钮。

Java 代码如下所示:

setContentView(R.layout.submit_select);
        RadioGroup radioGroup = (RadioGroup)findViewById(R.id.SelectRestaurant);
        for (String restaurant : restaurants) {
            RadioButton radioButton = new RadioButton(getBaseContext());
            radioButton.setText(restaurant);
            radioGroup.addView(radioButton);
        }
        radioGroup.invalidate();
        dismissDialog(DIALOG_SUBMIT_ID);
        rateItButton = (Button) findViewById(R.id.submitSelection);
        rateItButton.setOnClickListener(this);

最佳答案

尝试使用 WRAP_CONTENT 为宽度和高度创建布局参数,然后在将按钮添加到组时添加布局参数:

radiogroup.addView(newRadioButton, layoutParams);

关于android - 如何动态添加单选按钮而不丢失 View 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5452155/

相关文章:

android - 请求正确的权限以在收件箱中保存短信

java - 如何使用 Dagger 模块和 Robolectric 测试 Activity onResume() 上的模拟交互?

android - 如何减少android中radiogroup中radiobuttons之间的空间?

android - RadioButton Get(java.lang.IllegalStateException:selectedRadioButton不能为null)

Android 设计单选按钮

android以编程方式在radiogroup按钮之间添加填充

android - gcm android 中的应用程序 ID

Activity.showDialog(int,Bundle)上的java.lang.NoSuchMethodError?

android - 条码扫描 是否有用于获取产品详细信息等的 API?

java - 如何取消选中 radiogroup 中的单选按钮并收听操作