---
title: "自定义之checkbox"
author: "xun005"
date: "2013-01-08T09:16:48Z"
tags: []
categories: ["移动开发"]
slug: "checkbox"
draft: false
lastmod: "2013-01-08T09:16:48Z"
id: 1762695
url: "https://xun005.iteye.com/blog/1762695"
---

### []()android中自定义checkbox大小和图片 （转）

### []() 在编程过程中使用android自带的checkbox显示过大，在网上找了很多文章，终于使用自定义的checkbox使显示更加美观。   网上说：这个控件其实就是个TextView加了个图片，你只要做两张png的图片，在darwable中用xml定义好点击事件，再在你的控件上把这个当背景引进来就可以了。但是这样做了以后显示效果还是不佳。说说我的做法吧： 1、找两张图片http://findicons.com/search/checkbox#  分别为选中和没选中的。命名为checkbox和checkbox\_empty 2、在drawable中创建文件checkbox\_selector.xml：   **\[c-sharp]** [view plain](http://blog.csdn.net/yonghumingshishenme/article/details/6559741 "view plain")[copy](http://blog.csdn.net/yonghumingshishenme/article/details/6559741 "copy")[print](http://blog.csdn.net/yonghumingshishenme/article/details/6559741 "print")[?](http://blog.csdn.net/yonghumingshishenme/article/details/6559741 "?") &lt;?xml version="1.0" encoding="utf-8"?&gt;   &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;       &lt;item android:state\_checked="true"            android:drawable="@drawable/checkbox" /&gt;&lt;!--选中时效果--&gt;       &lt;item android:state\_checked="false"            android:drawable="@drawable/checkbox\_empty" /&gt;&lt;!--未选中时效果--&gt;     &lt;!-- 修改成你自己的图片就可以了 --&gt;   &lt;/selector&gt;   **\[c-sharp]** [view plain](/admin/blogs/ "view plain")[copy](/admin/blogs/ "copy")[print](/admin/blogs/ "print")[?](/admin/blogs/ "?") &lt;?xml version="1.0" encoding="utf-8"?&gt;   &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;       &lt;item android:state\_checked="true"            android:drawable="@drawable/checkbox" /&gt;&lt;!--选中时效果--&gt;       &lt;item android:state\_checked="false"            android:drawable="@drawable/checkbox\_empty" /&gt;&lt;!--未选中时效果--&gt;     &lt;!-- 修改成你自己的图片就可以了 --&gt;   &lt;/selector&gt;           注意：这里的状态是android:state\_checked 3、在values中创建styles.xml： &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;resources&gt;    &lt;style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox"&gt;    &lt;item name="android:button"&gt;@drawable/checkbox\_selector&lt;/item&gt;    &lt;item name="android:paddingLeft"&gt;25.0dip&lt;/item&gt;    &lt;item name="android:maxHeight"&gt;10.0dip&lt;/item&gt;    &lt;/style&gt; &lt;/resources&gt;   4、在你的CheckBox中添加属性：   **\[c-sharp]** [view plain](http://blog.csdn.net/yonghumingshishenme/article/details/6559741 "view plain")[copy](http://blog.csdn.net/yonghumingshishenme/article/details/6559741 "copy")[print](http://blog.csdn.net/yonghumingshishenme/article/details/6559741 "print")[?](http://blog.csdn.net/yonghumingshishenme/article/details/6559741 "?") &lt;CheckBox     android:layout\_width="wrap\_content"     android:layout\_height="wrap\_content"     style="@style/MyCheckBox"     /&gt;   **\[c-sharp]** [view plain](/admin/blogs/ "view plain")[copy](/admin/blogs/ "copy")[print](/admin/blogs/ "print")[?](/admin/blogs/ "?") &lt;CheckBox     android:layout\_width="wrap\_content"     android:layout\_height="wrap\_content"     style="@style/MyCheckBox"     /&gt;           经过以上步骤应该可以了。我对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 最后上个图吧：![](http://hi.csdn.net/attachment/201106/21/8985077_1308664284u6mC.png)