Answers:
是的,您可能必须在res / values / styles.xml中为单选按钮定义自己的样式:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme" parent="android:Theme">
<item name="android:radioButtonStyle">@style/RadioButton</item>
</style>
<style name="RadioButton" parent="@android:style/Widget.CompoundButton.RadioButton">
<item name="android:button">@drawable/radio</item>
</style>
</resources>
这里的“ radio”应该是有状态可绘制的radio.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
android:drawable="@drawable/radio_hover" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="@drawable/radio_normal" />
<item android:state_checked="true" android:state_pressed="true"
android:drawable="@drawable/radio_active" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="@drawable/radio_active" />
<item android:state_checked="true" android:state_focused="true"
android:drawable="@drawable/radio_hover" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="@drawable/radio_normal_off" />
<item android:state_checked="false" android:drawable="@drawable/radio_normal" />
<item android:state_checked="true" android:drawable="@drawable/radio_hover" />
</selector>
然后,将“自定义”主题应用于整个应用或您选择的活动。
有关主题和样式的更多信息,请参见http://brainflush.wordpress.com/2009/03/15/understanding-android-themes-and-styles/,这是一个很好的指南。
您可以像在普通按钮一样将自定义图像放在单选按钮中。为此,在可绘制文件夹中创建一个XML文件,例如
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/sub_screens_aus_hl"
android:state_pressed="true"/>
<item android:drawable="@drawable/sub_screens_aus"
android:state_checked="true"/>
<item android:drawable="@drawable/sub_screens_aus"
android:state_focused="true" />
<item android:drawable="@drawable/sub_screens_aus_dis" />
</selector>
在这里您可以为单选按钮使用3个不同的图像
并将此文件用于RadioButton,例如:
android:button="@drawable/aus"
android:layout_height="120dp"
android:layout_width="wrap_content"
仅更改单选按钮的更简单方法是将选择器设置为可绘制的右侧
<RadioButton
...
android:button="@null"
android:checked="false"
android:drawableRight="@drawable/radio_button_selector" />
选择器是:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_checkbox_checked" android:state_checked="true" />
<item android:drawable="@drawable/ic_checkbox_unchecked" android:state_checked="false" /></selector>
就这样
是的...来自Xml
android:button="@drawable/yourdrawable"
和从Java
myRadioButton.setButtonDrawable(resourceId or Drawable);
`
如果您想以编程方式进行操作,
checkBoxOrRadioButton.setButtonDrawable(null);
checkBoxOrRadioButton.setBackgroundResource(R.drawable.resource_name);