android 提供了许多控件,但是要想达到好的用户界面和美观效果,我们需要自定义控件才行。相信每一个android程序员都经历过学习自定义控件的过程。本人小白一枚,也是在不断的学习中。希望大家能和我一起学习。有问题也希望大家多多指教。
这篇文字转载自http://blog.csdn.net/zuolongsnail/article/details/7106586。谢谢前辈的指点。
最开始我总是以为自定义控件很难,后来看了下部分代码,发现自定义基本上都是这么几步。但是想要达到更好的点击效果可能会更复杂点。今天简单学习下CheckBox的自定义。下面看下代码。
第一步,
首先在drawable文件夹中添加drawable文件checkbox_style.xml。
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:drawable="@drawable/checkbox_pressed" android:state_checked="true"/>
- <item android:drawable="@drawable/checkbox_normal" android:state_checked="false"/>
- <item android:drawable="@drawable/checkbox_normal"/>
- </selector>
接下来就是第二步
在values文件夹下的styles.xml文件中添加CustomCheckboxTheme样式。
- <style name="CustomCheckboxTheme" parent="@android:style/Widget.CompoundButton.CheckBox">
- <item name="android:button">@drawable/checkbox_style</item>
- </style>
- <CheckBox
- android:id="@+id/select_all"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- style="@style/CustomCheckboxTheme" />
使用到的图片资源
checkbox_normal.png
checkbox_pressed.png
基本上自定义控件都是这三步,但是如果想达到更加好看的效果在第一步中可能副更加复杂点。随后我会发更多其他控件的自定义文章。希望大家多多指教。