自定义 RadioButton 选中和未选中时的图片

RadioButton长成什么样子是由其Background、Button等属性决定的,Android系统 
使用style定义了默认的属性,在android源码 
android/frameworks/base/core/res/res/values/styles.xml中可以看到默认的定义: 
Xml代码   收藏代码
  1. <style name="Widget.CompoundButton.RadioButton">  
  2.         <item name="android:background">@android:drawable/btn_radio_label_background</item>  
  3.         <item name="android:button">@android:drawable/btn_radio</item>  
  4. </style>  

即其背景图是btn_radio_label_background,其button的样子是btn_radio 

btn_radio_label_background是什么? 
其路径是android/frameworks/base/core/res/res/drawable-mdpi/btn_radio_label_background.9.png 
可以看到是一个NinePatch图片,用来做背景,可以拉伸填充。 

btn_radio是什么? 
其路径是android/frameworks/base/core/res/res/drawable/btn_radio.xml 
是个xml定义的drawable,打开看其内容: 
Xml代码   收藏代码
  1. <selector xmlns:android="http://schemas.android.com/apk/res/android">      
  2.     <item android:state_checked="true" android:state_window_focused="false"  
  3.           android:drawable="@drawable/btn_radio_on" />  
  4.     <item android:state_checked="false" android:state_window_focused="false"  
  5.           android:drawable="@drawable/btn_radio_off" />  
  6.   
  7.     <item android:state_checked="true" android:state_pressed="true"  
  8.           android:drawable="@drawable/btn_radio_on_pressed" />  
  9.     <item android:state_checked="false" android:state_pressed="true"  
  10.           android:drawable="@drawable/btn_radio_off_pressed" />  
  11.   
  12.     <item android:state_checked="true" android:state_focused="true"  
  13.           android:drawable="@drawable/btn_radio_on_selected" />  
  14.     <item android:state_checked="false" android:state_focused="true"  
  15.           android:drawable="@drawable/btn_radio_off_selected" />  
  16.   
  17.     <item android:state_checked="false" android:drawable="@drawable/btn_radio_off" />  
  18.     <item android:state_checked="true" android:drawable="@drawable/btn_radio_on" />  
  19. </selector>  

定义了不同状态下radioButton长成什么样子。 
如果不知道selector是什么,就要去看下Android SDK文档中Dev Guide->Application Resources->Resource Types。 
以下面一个item为例: 
<item android:state_checked="true" android:state_pressed="true" 
          android:drawable="@drawable/btn_radio_on_pressed" /> 
意思即为当radiobutton被选中时,并且被按下时,其Button应该长成btn_radio_on_pressed这个样子。 
 
文件是android/frameworks/base/core/res/res/drawable-mdpi/btn_radio_on_pressed.png 

drawable的item中可以有以下属性: 
android:drawable="@[package:]drawable/drawable_resource"
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_active=["true" | "false"]
android:state_checkable=["true" | "false"] 
android:state_checked=["true" | "false"] 
android:state_enabled=["true" | "false"] 
android:state_window_focused=["true" | "false"] 
当按钮的状态和某个item匹配后,就会使用此item定义的drawable作为按钮图片。 

从上面分析我们如果要修改RadioButton的外观,那么步骤应该是: 
(1)制作一个9patch的图片作为背景图 
准备一副PNG图片,其中白色为透明色,是否需要透明各人根据自己需要决定。 
 
运行SDK/tools/draw9patch 
在可伸缩的范围周围加上黑色的线告知系统这些区域可以伸缩。 
制作完的图片,周围多了黑色线。 
 
(2)针对不同的状态提供按钮图片 
enabled, on: 紫色外框、红色中心点 
 
enabled, off:只有紫色外框 
 
enabled, on, pressed:黄色外框,红色中心点 
 
enabled, off, pressed:黄色外框 
 
disabled, on: 灰色外框、灰色中心点 
 
disabled, off: 灰色外框 
 
其余的状态此处就不再定义。 
(3)使用xml描述一个drawable 
在res/drawable/创建custom_radio_btn.xml 
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:state_enabled="true" android:state_checked="true" android:state_pressed="true"  
  4.           android:drawable="@drawable/enabled_on_pressed" />  
  5.             
  6.     <item android:state_enabled="true" android:state_checked="false" android:state_pressed="true"  
  7.           android:drawable="@drawable/enabled_off_pressed" />  
  8.   
  9.     <item android:state_enabled="true" android:state_checked="true"  
  10.           android:drawable="@drawable/enabled_on" />  
  11.             
  12.     <item android:state_enabled="true" android:state_checked="false"  
  13.           android:drawable="@drawable/enabled_off" />  
  14.             
  15.     <item android:state_enabled="false" android:state_checked="true"  
  16.           android:drawable="@drawable/disabled_on" />  
  17.             
  18.     <item android:state_enabled="false" android:state_checked="false"  
  19.           android:drawable="@drawable/disabled_off" />                
  20. </selector>  

Item顺序是有讲究的,条件限定越细致,则应该放到前面。比如这儿如果把1,2行和3,4行的item交换,那么pressed的就永远无法触发了,因为有item已经满足条件返回了。可以理解为代码中的if语句。 
(4)创建一个自定义的style,并应用到RaidioButton的style属性上 
Xml代码   收藏代码
  1. <style name="CustomRadioBtn">  
  2.         <item name="android:background">@drawable/radio_btn_bg</item>  
  3.         <item name="android:button">@drawable/custom_radio_btn</item>  
  4. </style>  


运行ap即可看到此RadioButton的外观已经改变,此demo可以看到文字被按钮遮盖了一部分, 
这儿是因第一步制作9patch图片时没有留出按钮图片空间来,稍作修改即可。 

 


转自:http://mypyg.iteye.com/blog/768471


































  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
如果要实现不在同一个 RadioGroup 的两个 RadioButton 只能选中其中一个的话,可以通过以下两种方式来实现: 1. 使用 RadioGroup 和 LinearLayout 组合实现: 在布局文件中,将两个 RadioButton 放在同一个 LinearLayout 中,然后再将 LinearLayout 放在 RadioGroup 中,这样就可以实现两个 RadioButton 只能选中其中一个的效果。具体代码如下: ```xml <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton 1" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton 2" /> </LinearLayout> </RadioGroup> ``` 2. 使用自定义属性实现: 在布局文件中,给两个 RadioButton 设置同一个自定义属性,然后在代码中监听两个 RadioButton 的点击事件,当一个 RadioButton选中,将另一个 RadioButton选中状态设置为选中。具体代码如下: 在 res/values/attrs.xml 文件中定义自定义属性: ```xml <declare-styleable name="CustomRadioButton"> <attr name="exclusive" format="boolean" /> </declare-styleable> ``` 在布局文件中,给两个 RadioButton 设置相同的自定义属性: ```xml <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton 1" app:exclusive="true" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton 2" app:exclusive="true" /> ``` 在代码中,监听两个 RadioButton 的点击事件,并在事件中将另一个 RadioButton选中状态设置为选中: ```java RadioButton radioButton1 = findViewById(R.id.radioButton1); RadioButton radioButton2 = findViewById(R.id.radioButton2); radioButton1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { radioButton2.setChecked(false); } }); radioButton2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { radioButton1.setChecked(false); } }); ``` 以上是两种实现不在同一个 RadioGroup 的两个 RadioButton 只能选中其中一个的方法,您可以根据自己的需求选择其中一种。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值