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

Posted

技术标签:

【中文标题】是否可以更改android单选按钮组中的单选按钮图标【英文标题】:Is it possible to change the radio button icon in an android radio button group 【发布时间】:2011-04-04 08:09:54 【问题描述】:

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

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

【问题讨论】:

【参考方案1】:

如果您想以编程方式进行,

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

【讨论】:

【参考方案2】:

是的....` 来自 Xml

android:button="@drawable/yourdrawable" 

来自 Java

myRadioButton.setButtonDrawable(resourceId or Drawable);

`

【讨论】:

【参考方案3】:

仅更改单选按钮的更简单方法是简单地将选择器设置为可绘制右侧

<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>

就是这样

【讨论】:

最佳解决方案,就我而言,我还将 'drawableRight' 更改为 'button' 并在选择器 xml 中添加了一些填充 这是最好的解决方案。我曾以这种方式使用它,它工作正常。但是在迁移到 andoidx android:button="@null" 后不起作用,我们看到每个项目都有两个单选按钮图标! .【参考方案4】:

这可能是一种快速的方法,

使用上面显示的两个图标,您应该有一个类似这样的RadioGroup

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

【讨论】:

【参考方案5】:

您可以像普通按钮一样将自定义图像放在单选按钮中。 为此在可绘制文件夹中创建一个 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_
android:layout_ 

【讨论】:

【参考方案6】:

是的,您可能必须在 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>

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

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

【讨论】:

您也可以只在单选按钮本身上设置 android:button 属性,而不是为其定义单独的自定义样式。 没错,但这不是很好的做法,因为您通常只有一个以上的单选按钮。 @KonstantinBurov 如果我使用的是 9 补丁图像怎么办? @KonstantinBurov 除非您以编程方式添加按钮,在这种情况下它很棒:) 对于支持主题可能要使用

以上是关于是否可以更改android单选按钮组中的单选按钮图标的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式更改表格单元格中单选按钮组的单选按钮?

选择组中的下一个单选按钮

如何处理 ExpandableListView 中的单选按钮?

如何检查单选按钮是不是在 Android 的单选组中被选中?

AngularJs:如何设置基于模型检查的单选按钮

当用户从颤动的单选按钮组中选择单选按钮时如何保存数据?