我想創建一個RadioGroup,每個單選按鈕的「未選中」狀態都有不同的顏色。我可以使用相同的「radioButton_states」drawable並創建每個按鈕具有不同背景色的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)
?