如何将可绘制 xml 文件添加到使用 For 循环创建的单选按钮中
how to add drawable xml file into radiobutton that is created using For loop
我知道这是个愚蠢的问题,但我搜索了 2 天但无法得到这个问题的答案
我自定义了 RadioButton,因此它看起来不错,并且在我将可绘制文件用于布局时它正在工作,但我不知道如何使用/将这些文件放入我的代码中。现在 RadioButton 正在使用 For Loop
创建
这是我如何创建 RadioButton
for(int i =0; i<ab.length;i++)
{
RadioButton radioButtonView = new RadioButton(this);
radioButtonView.setText(ab[i]);
radioGroup.addView(radioButtonView, p);
}
但我可以在这样的布局中使用 xml 个文件
<RadioButton
android:id="@+id/radioAndroid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rbtn_selector"
android:textColor="@drawable/rbtn_textcolor_selector" />
任何人都可以建议如何将这些文件放入我的 for 循环中,以便 RadioButton 看起来不错
谢谢
您可以通过编程方式使用 setBackgroundResource,例如:
for(int i =0; i<ab.length;i++)
{
RadioButton radioButtonView = new RadioButton(this);
radioButtonView.setText(ab[i]);
XmlResourceParser parser = getResources().getXml(
R.drawable.rbtn_textcolor_selector);
ColorStateList colors = ColorStateList.createFromXml(getResources(),
parser);
radioButtonView.setTextColor(colors);
radioButtonView.setBackgroundResource(R.drawable.rbtn_selector);
radioGroup.addView(radioButtonView, p);
}
使用这个
for(int i =0; i<ab.length;i++)
{
RadioButton radioButtonView = new RadioButton(this);
radioButtonView.setText(ab[i]);
radioButtonView.setTextColor(R.color.color_name);
radioButtonView.setBackGroundResource(R.drawable.rbtn_selector);
radioGroup.addView(radioButtonView, p);
}
我知道这是个愚蠢的问题,但我搜索了 2 天但无法得到这个问题的答案
我自定义了 RadioButton,因此它看起来不错,并且在我将可绘制文件用于布局时它正在工作,但我不知道如何使用/将这些文件放入我的代码中。现在 RadioButton 正在使用 For Loop
创建
这是我如何创建 RadioButton
for(int i =0; i<ab.length;i++)
{
RadioButton radioButtonView = new RadioButton(this);
radioButtonView.setText(ab[i]);
radioGroup.addView(radioButtonView, p);
}
但我可以在这样的布局中使用 xml 个文件
<RadioButton
android:id="@+id/radioAndroid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rbtn_selector"
android:textColor="@drawable/rbtn_textcolor_selector" />
任何人都可以建议如何将这些文件放入我的 for 循环中,以便 RadioButton 看起来不错 谢谢
您可以通过编程方式使用 setBackgroundResource,例如:
for(int i =0; i<ab.length;i++)
{
RadioButton radioButtonView = new RadioButton(this);
radioButtonView.setText(ab[i]);
XmlResourceParser parser = getResources().getXml(
R.drawable.rbtn_textcolor_selector);
ColorStateList colors = ColorStateList.createFromXml(getResources(),
parser);
radioButtonView.setTextColor(colors);
radioButtonView.setBackgroundResource(R.drawable.rbtn_selector);
radioGroup.addView(radioButtonView, p);
}
使用这个
for(int i =0; i<ab.length;i++)
{
RadioButton radioButtonView = new RadioButton(this);
radioButtonView.setText(ab[i]);
radioButtonView.setTextColor(R.color.color_name);
radioButtonView.setBackGroundResource(R.drawable.rbtn_selector);
radioGroup.addView(radioButtonView, p);
}