android 下改变默认的checkbox的 选中 和被选中 图片

来源:互联网 发布:hit韩服数据 编辑:程序博客网 时间:2024/05/23 19:24

大部分情况下,我们在UI中并不采用Android自带的checkbox复选框样式,这时候就需要我们自定义自己的checkbox。


首先找两张checkbox背景图片,比如下图样子的:



然后在drawable目录下定义一个背景图片xml文件,内容如下:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector  
  3.   xmlns:android="http://schemas.android.com/apk/res/android">  
  4.     <item android:state_checked="true" android:drawable="@drawable/fuxuan_input01" />  
  5.     <item android:state_selected="true" android:drawable="@drawable/fuxuan_input01"></item>  
  6.     <item android:state_pressed="true" android:drawable="@drawable/fuxuan_input01"></item>  
  7.     <item android:state_checked="false" android:drawable="@drawable/fuxuan_input02" />  
  8. </selector>  


在需要用到checkbox的地方定义它,属性设置如下代码所示:

[html] view plain copy
  1. <CheckBox  
  2.     android:id="@+id/agree_provision_chk"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="@drawable/chk_agee_provision_background"  
  6.     android:button="@null" />  

自定义checkbox就此完成,效果如下图所示:

0 0
原创粉丝点击