首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >是否可以更改android单选按钮组中的单选按钮图标

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

Stack Overflow用户
提问于 2010-08-26 23:11:02
回答 5查看 110.8K关注 0票数 97

我想让我的android应用程序的用户能够设置一些参数。对于这种情况,单选按钮是理想的。但是,我不喜欢呈现单选按钮。

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

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-08-26 23:37:10

是的,这是可能的,你必须为单选按钮定义你自己的样式,在res/values/styles.xml:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
    <?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/,这是一个很好的指南。

票数 169
EN

Stack Overflow用户

发布于 2011-10-01 20:05:23

您可以将自定义图像放在单选按钮中,就像普通按钮。为此,请在可绘制文件夹中创建一个XML文件,例如

代码语言:javascript
复制
<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命令:

代码语言:javascript
复制
android:button="@drawable/aus"
android:layout_height="120dp"
android:layout_width="wrap_content" 
票数 32
EN

Stack Overflow用户

发布于 2016-03-12 20:23:22

只需将选择器设置为可绘制右侧,就可以更简单地更改单选按钮

代码语言:javascript
复制
<RadioButton
    ...
    android:button="@null"
    android:checked="false"
    android:drawableRight="@drawable/radio_button_selector" />

选择器是:

代码语言:javascript
复制
<?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>

就这样

票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3576507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档