【问题标题】:add radio group by dynamically but radio button is xml动态添加单选组,但单选按钮是 xml
【发布时间】:2016-05-24 11:49:34
【问题描述】:

您好,我有两个单选按钮,我需要动态添加单选组。 这个怎么做? 这是我的xml,谢谢

                    <RadioButton
                        android:id="@+id/radiotwo"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:checked="false"
                        android:text="b"
                        android:textSize="23dp" />
                   <RadioButton
                        android:id="@+id/radioone"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:checked="false"
                        android:text="a"
                        android:textSize="23dp" />

【问题讨论】:

  • 那你为什么不动态添加单选组和单选按钮呢?

标签: android radio-button radio-group


【解决方案1】:

假设你要添加的布局是LinearLayout,你可以像这样添加一个RadioButton:

LinearLayout myLayout = findViewById(R.id.main);
RadioButton myButton = new RadioButton(this);
myButton.setLayoutParams(new LinearLayout.LayoutParams(
                                 LinearLayout.LayoutParams.FILL_PARENT,
                                 LinearLayout.LayoutParams.FILL_PARENT));
myLayout.addView(myButton);

【讨论】:

    猜你喜欢
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 2016-06-10
    • 2023-03-15
    • 2012-05-20
    • 2018-11-06
    相关资源
    最近更新 更多