【问题标题】:How to dynamically add radiobuttons and not lose view contents如何动态添加单选按钮而不丢失视图内容
【发布时间】:2011-03-27 20:25:30
【问题描述】:

我的布局是这样的

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

【问题讨论】:

  • “单选按钮相互重叠,提交按钮不见了” - 有点不清楚,你能张贴截图吗?我想因为你没有使用ScrollView,所以按钮消失了,因为它被推出了屏幕的可见部分。然而,如果没有截图,这只是一个猜测。

标签: android radio-group


【解决方案1】:

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

radiogroup.addView(newRadioButton, layoutParams);

【讨论】:

    猜你喜欢
    • 2015-07-15
    • 2023-01-09
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2010-10-02
    相关资源
    最近更新 更多