我希望让我的安卓应用的用户能够设置一些参数,单选按钮是这种情况下的理想选择。但是,我不喜欢单选按钮的样式。
有没有可能改变单选按钮的图标?例如,是否可以为每行创建自定义布局,在该布局中引用自己的图标并更改字体等内容。
我希望让我的安卓应用的用户能够设置一些参数,单选按钮是这种情况下的理想选择。但是,我不喜欢单选按钮的样式。
有没有可能改变单选按钮的图标?例如,是否可以为每行创建自定义布局,在该布局中引用自己的图标并更改字体等内容。
是的,这是可能的,您需要在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/,这是一个很好的指南。
您可以像普通按钮一样在单选按钮中设置自定义图像。 为此,请在drawable文件夹中创建一个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"
只更改单选按钮的简便方法是将选择器设置为Drawable Right。
<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);