android - 是否可以更改android单选按钮组中的单选按钮图标

我想让我的 android 应用程序的用户能够设置一些参数。单选按钮非常适合这种情况。但是,我不喜欢渲染单选按钮。

是否可以更改单选按钮图标?例如,是否可以为每一行创建自定义布局,并在该布局中引用我自己的图标并更改字体等。

最佳答案

是的,您可能必须在 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'应该是一个有状态的drawable,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>

然后只需将自定义主题应用于整个应用或您选择的 Activity 。

有关主题和样式的更多信息,请查看 http://brainflush.wordpress.com/2009/03/15/understanding-android-themes-and-styles/这是一个很好的指南。

https://stackoverflow.com/questions/3576507/

相关文章:

android - 如何查看 Google Play 服务版本?

android - 在非 Activity 类中获取上下文

android - 以编程方式获取设备的 Android API 级别?

android - 在 Android 中通过 Canvas 创建一个空位图并进行绘图

android - post方法到底是做什么的?

android - 如何在 Android 中模拟触摸事件?

android - 将 largeHeap 设置为 true 有什么好处?

android - 从 android 图库中选择多张图片

android - 如何在 Android 中更改代理设置(尤其是在 Chrome 中)

android - 了解 keystore 、证书和别名