我可以使用相同的“radioButton_states”drawable并创建一个每个按钮具有不同背景颜色的radioGroup吗?

问题描述 投票:2回答:1

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

例如。 enter image description here

目前我有。

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

 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包含已选中和未选中的drawable。

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>

这产生的是:

enter image description here

似乎颜色永远不会被保存,radio_button_states只使用最后设置的颜色。

我是否需要为每个按钮设置不同的drawable button_states?或者有没有办法可以将每个彩色背景保存到每个radioButton的setBackgroundResource(R.drawable.radio_button_states)

android xml radio-button customization radio-group
1个回答
1
投票

所有资源抽屉(R.drawable。*)本质上都是静态的,因此当您使用getPaint().setColor(color)为一个RadioButton更改颜色时,它将针对所有这些更改。

我无法完全掌握你在使用instanceOf代码所做的事情,因为看起来你只是使用形状Drawables,所以我没有一个完整的解决方案......但我有一个方法给你。

而不是使用Resource Drawables,为每个按钮创建一个新的Drawable实例。例如,将private 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以获得效果)。

My emulator with your code, tweaked

© www.soinside.com 2019 - 2024. All rights reserved.