我的布局是这样的
<?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/