【发布时间】:2021-04-02 05:41:56
【问题描述】:
我想在一个片段中动态地创建一些单选按钮,我只有样式问题。如果我将单选按钮代码放在 xml 文件中,默认样式会正确应用,但是当我通过函数创建单选按钮时,我会看到不同的样式!
XML
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:animationCache="false">
<RadioButton
android:text="RadioButton 1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radioButton3" />
<RadioButton
android:text="RadioButton 2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radioButton4" />
</RadioGroup>
结果
JAVA 代码
这段代码放在fragment中的onCreateView中
public void addRadioButton(Context ctx,int num){
RadioGroup radioGroup= (RadioGroup) alertInflatedView.findViewById(R.id.radiogroup);
for (int i = 1; i <= num; i++) {
RadioButton radioButton = new RadioButton(ctx);
radioButton.setId(1+i);
radioButton.setText("Radio " + radioButton.getId());
radioButton.setTextColor(getResources().getColor(R.color.black));
radioGroup.addView(radioButton);
}
}
结果
您可以看到单选按钮具有不同的样式,如果可能的话,有人可以帮助我以编程方式应用默认样式吗?
【问题讨论】:
标签: android android-radiobutton