我可以使用相同的 "radioButton_states" 可绘制对象并创建一个每个按钮具有不同背景颜色的 radioGroup 吗?

Can I use the same"radioButton_states" drawable and create a radioGroup that has different background colors each button?

我正在尝试为每个单选按钮 "unchecked" 状态创建一个具有不同颜色的 radioGroup。

例如。

目前我有。

CreateRadioButtonsRow() 尝试在 linearLayout 中创建一行 RadioButtons 作为 RadioGroup

 ColorChooser.java (important function from class)

private Dialog colorChooserDialog;
private Context context;

private LinearLayout linearLayoutColors;
private int[] colorChooserColors;
private Drawable radioButtonBackground;


 private void CreateRadioButtonRow()
{
    final RadioButton [] radioButtons = new RadioButton[6];
    RadioGroup radioGroup = new RadioGroup(context);
    radioGroup.setOrientation(RadioGroup.HORIZONTAL);

    for (int i = 0; i < 5; i++)
    {
        radioButtons[i] = new RadioButton(context);
        setDrawableBackgroundColor(colorChooserColors[i]);
        radioButtons[i].setBackgroundResource(R.drawable.radio_button_states);
        radioButtons[i].setButtonDrawable(R.drawable.null_selector);
        radioGroup.addView(radioButtons[i]);
    }
    linearLayoutColors.addView(radioGroup);
}

private void setDrawableBackgroundColor(int color)
{
    if (radioButtonBackground instanceof ShapeDrawable)
    {
        ((ShapeDrawable)radioButtonBackground).getPaint().setColor(color);
    }
    else if (radioButtonBackground instanceof GradientDrawable)
    {
        ((GradientDrawable)radioButtonBackground).setColor(color);
    }
}

radio_button_states.xml 包含选中和未选中的可绘制对象。

radio_button_states.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"
        android:drawable="@drawable/color_radio_button_enabled"/>

    <item android:state_checked="false"
        android:drawable="@drawable/color_radio_button_background"/>
</selector>

color_radio_button_enabled.xml 是启用状态,它基本上是未选中的圆形,周围有一个白色环。

color_radio_button_enabled.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/color_radio_button_background"/>
    <item android:drawable="@drawable/selected_ring"/>
</layer-list>

Selected_Ring.xml是白环层

selected_ring.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:innerRadiusRatio="2"
    android:thickness="2dp"
    android:useLevel="false">
    <size
        android:width="40dp"
        android:height="40dp" />
    <solid
        android:color="#ffffff"/>
</shape>

color_radio_button_background.xml是彩色圆圈。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <size android:width="40dp"
          android:height="40dp"/>
</shape>

colors.xml用于填充int[]colorChooserColors

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="c0" type="color">#ff9999</item>
    <item name="c1" type="color">#ff4d4d</item>
    <item name="c2" type="color">#ff0000</item>
    <item name="c3" type="color">#cc0000</item>
    <item name="c4" type="color">#990000</item>
    <item name="c5" type="color">#660000</item>
    <integer-array name="array_chooser_colors">
        <item>@color/c0</item>
        <item>@color/c1</item>
        <item>@color/c2</item>
        <item>@color/c3</item>
        <item>@color/c4</item>
        <item>@color/c5</item>
    </integer-array>
</resources>

这产生的是:

似乎颜色永远不会保存,radio_button_states 只使用上次设置的颜色。

我需要为每个按钮设置不同的可绘制对象 button_states 吗?或者有什么方法可以将每个彩色背景保存到每个 radioButton 的 setBackgroundResource(R.drawable.radio_button_states)?

所有 Resource Drawables (R.drawable.*) 本质上都是静态的,所以当你改变颜色时使用getPaint().setColor(color) 对于一个 RadioButton,它会为所有它们更改。

我无法完全理解您使用 instanceOf 代码所做的事情,因为看起来您只使用了形状 Drawables,所以我不知道'有一个完整的解决方案......但我有一个方法适合你。

与其使用 Resource Drawables,不如为每个按钮创建一个新的 Drawable 实例。例如,更改私有 void setDrawableBackgroundColor(int color) 以接受 RadioButton,并即时附加新背景 Drawable。看这里:

//------------------
for (int i = 0; i < 5; i++)
{
    radioButtons[i] = new RadioButton(context);

    //The line below is undoing the setDrawableBackgroundColor line
    //radioButtons[i].setBackgroundResource(R.drawable.radio_button_states);

    //Didn't have the file for this one...
    //radioButtons[i].setButtonDrawable(R.drawable.null_selector);

    //Notice how now we pass a RadioButton to the below method
    setDrawableBackgroundColor(colorChooserColors[i], radioButtons[i]);

    radioGroup.addView(radioButtons[i]);
}
//------------------
private void setDrawableBackgroundColor(int color, RadioButton radio) {

    //We are now creating a new ShapeDrawable instance for each individual 
    //button - so now they will all have their own individual Drawable, 
    //rather than sharing the same static one.

    Drawable radioButtonBackground = new ShapeDrawable(new OvalShape());

    ((ShapeDrawable) radioButtonBackground).getPaint().setColor(color);

    radio.setBackground(radioButtonBackground);
}

我认为这不会完全解决您的问题,但我希望它能为您指明正确的方向。我尝试了上面的代码并得到了以下结果(为了效果我稍微调整了 colors.xml)。