3

我的布局看起来像这样

    <?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 文件中的单选组时,单选按钮相互重叠并且提交按钮消失了。我猜单选按钮正在覆盖其他视图元素。如何在不丢失其他元素的情况下动态添加这些单选按钮。

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);
4

1 回答 1

2

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

radiogroup.addView(newRadioButton, layoutParams);
于 2011-09-02T17:07:00.470 回答