android自定义空间之自定义CheckBox

android 提供了许多控件,但是要想达到好的用户界面和美观效果,我们需要自定义控件才行。相信每一个android程序员都经历过学习自定义控件的过程。本人小白一枚,也是在不断的学习中。希望大家能和我一起学习。有问题也希望大家多多指教。

这篇文字转载自http://blog.csdn.net/zuolongsnail/article/details/7106586。谢谢前辈的指点。

最开始我总是以为自定义控件很难,后来看了下部分代码,发现自定义基本上都是这么几步。但是想要达到更好的点击效果可能会更复杂点。今天简单学习下CheckBox的自定义。下面看下代码。

第一步,

首先在drawable文件夹中添加drawable文件checkbox_style.xml。

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


接下来就是第二步

在values文件夹下的styles.xml文件中添加CustomCheckboxTheme样式。

[html]  view plain copy
  1. <style name="CustomCheckboxTheme" parent="@android:style/Widget.CompoundButton.CheckBox">  
  2.     <item name="android:button">@drawable/checkbox_style</item>  
  3. </style> 
第三步 .在布局文件中使用CustomCheckboxTheme样式。

[html]  view plain copy
  1. <CheckBox  
  2.         android:id="@+id/select_all"  
  3.         android:layout_width="wrap_content"  
  4.         android:layout_height="wrap_content"  
  5.         style="@style/CustomCheckboxTheme" />  

使用到的图片资源

checkbox_normal.png

checkbox_pressed.png



基本上自定义控件都是这三步,但是如果想达到更加好看的效果在第一步中可能副更加复杂点。随后我会发更多其他控件的自定义文章。希望大家多多指教。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值