---
title: "自定义CheckBox和RadioButton"
author: "何不笑"
date: "2012-10-10T21:39:07Z"
tags: ["checkbox"]
categories: ["移动开发"]
slug: "checkbox-radiobutton"
draft: false
lastmod: "2012-10-10T21:39:07Z"
id: 1695376
url: "https://wait7758521.iteye.com/blog/1695376"
---

1 在drawable-mdpi文件夹下创建文件checkbox\_selector.xml：

 

&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/login\_checked" /&gt;&lt;!--选中时效果--&gt;

&lt;item android:state\_checked="false"

android:drawable="@drawable/login\_unchecked" /&gt;&lt;!--未选中时效果--&gt;

&lt;/selector&gt;

 

2 在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;!-- 将原来系统的CheckBox图标隐藏起来。 --&gt;

&lt;item name="android:button"&gt;@null&lt;/item&gt;

 

&lt;!-- 设置CheckBox的左间距 --&gt;

&lt;item name="android:paddingLeft"&gt;10dip&lt;/item&gt;

 

&lt;!-- 设置CheckBox的最大高度 --&gt;

&lt;item name="android:maxHeight"&gt;50.0dip&lt;/item&gt;

 

&lt;!-- 设置文字和图标的间距 --&gt;

&lt;item name="android:drawablePadding"&gt;50dp&lt;/item&gt;

 

&lt;!-- 在CheckBox的右侧添加自定义的图标 --&gt;

&lt;item name="android:drawableRight"&gt;@drawable/checkbox\_selector&lt;/item&gt;

 

&lt;/style&gt;

 

&lt;/resources&gt;

 

3 在xml文件的CheckBox中添加属性 ：

 

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout\_width="fill\_parent"

android:layout\_height="fill\_parent"

android:orientation="vertical" &gt;

 

&lt;TextView

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

android:text="自定义CheckBox和RadioButton"

android:gravity="center\_horizontal"

android:paddingBottom="50dp"/&gt;

 

&lt;CheckBox

android:id="@+id/check\_box"

android:layout\_width="wrap\_content"

android:layout\_height="wrap\_content"

style="@style/MyCheckBox" &lt;!-- 引用自定义样式 --&gt;

android:text="good boy"

android:textSize="20sp"

android:textScaleX="1.0" &lt;!-- 设置文字间距 --&gt;

/&gt;

 

&lt;RadioGroup

android:id="@+id/radioGroup1"

android:layout\_width="match\_parent"

android:layout\_height="wrap\_content" &gt;

 

&lt;RadioButton

android:id="@+id/button1"

android:layout\_width="match\_parent"

android:layout\_height="wrap\_content"

android:button="@null"

android:checked="true"

android:drawableRight="@android:drawable/btn\_radio"

android:paddingLeft="10dp"

android:text="RadioButton" /&gt;

 

&lt;RadioButton

android:id="@+id/button2"

android:layout\_width="400dp"

android:layout\_height="wrap\_content"

android:button="@null"

android:drawableRight="@android:drawable/btn\_radio"

android:paddingLeft="10dp"

android:text="RadioButton" /&gt;

 

&lt;/RadioGroup&gt;

 

&lt;/LinearLayout&gt;

 

最后效果如下图：

![](http://dl.iteye.com/upload/attachment/0074/8312/f6354ade-4862-326d-8004-66e225e81c1b.png)

 

注意：CheckBox图标的大小只由被援引图片的本身大小决定，无法在java程序的xml中进行设定。所以为适应多种尺寸和密度的屏幕，应创建drawable-normal-mdpi、drawable-normal-hdpi、drawable-large-xhdpi和相应values-normal-mdpi、values-normal-hdpi、values-large-xhdpi等多个资源文件夹并放入相应的资源文件和图片，如下图所示：

 

![](http://dl.iteye.com/upload/attachment/0074/8315/defd60e4-a60f-336f-a8a8-0456a87b908e.jpg)