2015-11-27 38 views
2

我想創建一個RadioGroup,每個單選按鈕的「未選中」狀態都有不同的顏色。我可以使用相同的「radioButton_states」drawable並創建每個按鈕具有不同背景色的radioGroup嗎?

例如, enter image description here

目前我有。

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> 

這是什麼產生是:

enter image description here

看來,顏色永遠不會被保存radio_button_states只使用最後設置的顏色。

我是否需要爲每個按鈕設置不同的可繪製button_states?或者有沒有辦法將每個彩色背景保存到每個radioButton的setBackgroundResource(R.drawable.radio_button_states)

回答

1

所有資源可繪製 S(R.drawable。*)本質上是靜態的,所以當你改變使用getPaint().setColor(color)的顏色代表一個單選按鈕,它將爲所有這些改變。

我沒能完全掌握你與instanceOf代碼做什麼,因爲它看起來像你只使用形狀可繪製 S,所以我沒有一個完整的解決方案...但我有一個爲你的方法。

而不是使用資源可繪,爲每個按鈕創建一個新的可繪製實例。例如,將私人空白setDrawableBackgroundColor(int color)更改爲接受RadioButton,並附加一個新的背景可繪製的在飛行中。看到這裏:

//------------------ 
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

相關問題