简体   繁体   English

动态添加单选组,但单选按钮为xml

[英]add radio group by dynamically but radio button is xml

hi i have two radiobutton, i need to add radiogroup in dynamically. 您好,我有两个单选按钮,我需要动态添加单选按钮。 how to do this? 这个怎么做? this is my xml,thanks 这是我的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" />

Assuming the layout in witch you want to add it is a LinearLayout, you can add a RadioButton like this: 假设您要添加的布局是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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM