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

yam*_*pog 95 android radio-button

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

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

Kon*_*rov 166

是的,您可能需要在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>
Run Code Online (Sandbox Code Playgroud)

这里的'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>
Run Code Online (Sandbox Code Playgroud)

然后只需将自定义主题应用于整个应用程序或您选择的活动.

有关主题和样式更多信息看http://brainflush.wordpress.com/2009/03/15/understanding-android-themes-and-styles/这是很好的指导意义.

  • 您也可以在单选按钮本身上设置android:button属性,而不是为它定义单独的自定义样式. (13认同)
  • 没错,但这不是太好的练习,因为你通常只有一个单选按钮. (7认同)

Chi*_*rag 30

您可以像普通按钮一样将自定义图像放在radiobutton中.为此在可绘制文件夹中创建一个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> 
Run Code Online (Sandbox Code Playgroud)

在这里,您可以使用3个不同的图像进行无线电按钮

并将此文件用于RadioButton,如:

android:button="@drawable/aus"
android:layout_height="120dp"
android:layout_width="wrap_content" 
Run Code Online (Sandbox Code Playgroud)


小智 10

只更改单选按钮的简单方法就是为drawable right设置选择器

<RadioButton
    ...
    android:button="@null"
    android:checked="false"
    android:drawableRight="@drawable/radio_button_selector" />
Run Code Online (Sandbox Code Playgroud)

选择器是:

<?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>
Run Code Online (Sandbox Code Playgroud)

就这样

  • 最佳解决方案,对于我的情况,我还将 'drawableRight' 更改为 'button' 并向选择器 xml 添加了一些填充 (2认同)