在Android单选按钮组中更改单选按钮图标是否可能?

103

我希望让我的安卓应用的用户能够设置一些参数,单选按钮是这种情况下的理想选择。但是,我不喜欢单选按钮的样式。

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

6个回答

171

是的,这是可能的,您需要在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/,这是一个很好的指南。


14
你可以直接在单选按钮上设置android:button属性,而不需要为其定义一个单独的自定义样式。 - Yoni Samlan
7
没错,但这并不是一个好的做法,因为通常你不止有一个单选按钮。 - Konstantin Burov
@KonstantinBurov 如果我使用了9 patch图片,会怎样? - Hades
@KonstantinBurov,除非您正在通过编程方式添加按钮,在这种情况下它非常好:) - Cornholio
1
为了支持主题,您可能想要使用以下代码:<style name="CustomTheme" parent="android:Theme"> <item name="android:radioButtonStyle">@style/RadioButton</item> <item name="radioButtonStyle">@style/RadioButton</item> </style> - Dhruv

32

您可以像普通按钮一样在单选按钮中设置自定义图像。 为此,请在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" 

20

只更改单选按钮的简便方法是将选择器设置为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>

就这些了


2
最佳解决方案,对于我的情况,我还将“drawableRight”更改为“button”,并在选择器xml中添加了一些填充。 - F-1
这是最好的解决方案。我曾经使用过这种方式,它运行良好。但是在迁移到androidx后,android:button="@null"不起作用,我们会看到每个项目有两个单选按钮图标! - maniaq

4

是的……从XML

android:button="@drawable/yourdrawable" 

以及来自Java

myRadioButton.setButtonDrawable(resourceId or Drawable);

`


0

这里可能有一个快速的方法,

enter image description here

在上面显示了两个图标,你应该有一个类似这样的RadioGroup

enter image description here

  • RadioGroup的方向改为水平
  • 对于每个RadioButton的属性,尝试在CompoundButton下给出Button的图标
  • 调整填充和大小
  • 并在选中时设置背景属性。

0

如果你想以编程的方式实现它,

checkBoxOrRadioButton.setButtonDrawable(null);
checkBoxOrRadioButton.setBackgroundResource(R.drawable.resource_name);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接