在編程過程中使用android內建的checkbox顯示過大,在網上找了很多文章,終於使用自訂的checkbox使顯示更加美觀。
網上說:這個控制項其實就是個TextView加了個圖片,你只要做兩張png的圖片,在darwable中用xml定義好點擊事件,再在你的控制項上把這個當背景引進來就可以了。但是這樣做了以後顯示效果還是不佳。說說我的做法吧:
1、找兩張圖片http://findicons.com/search/checkbox# 分別為選中和沒選中的。命名為checkbox和checkbox_empty
2、在drawable中建立檔案checkbox_selector.xml:
<?xml version="1.0" encoding="utf-8"?><br /><selector xmlns:android="http://schemas.android.com/apk/res/android"><br /><item android:state_checked="true"<br /> android:drawable="@drawable/checkbox" /><!--選中時效果--><br /><item android:state_checked="false"<br /> android:drawable="@drawable/checkbox_empty" /><!--未選中時效果--><br /> <!-- 修改成你自己的圖片就可以了 --><br /></selector>
注意:這裡的狀態是android:state_checked
3、在values中建立styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/checkbox_selector</item>
<item name="android:paddingLeft">25.0dip</item>
<item name="android:maxHeight">10.0dip</item>
</style>
</resources>
4、在你的CheckBox中添加屬性:
<CheckBox<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> style="@style/MyCheckBox"<br /> />
經過以上步驟應該可以了。我對style和selector的使用也不熟悉,大家一起學習~
這裡查過的資料有:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=70428
http://topic.csdn.net/u/20101030/12/705ede36-c873-4d07-af0d-34c6b9145480.html
http://blog.sina.com.cn/s/blog_7898b0530100rfgs.html
最後上個圖吧: