首页 > 技术文章 > android中checkbox自定义样式

guop 2015-12-30 15:32 原文

1、首先res/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/check_true" android:state_checked="true"></item>  
    <item android:drawable="@drawable/check_true" android:state_selected="true"></item>  
    <item android:drawable="@drawable/check_true" android:state_pressed="true"></item>  
    <item android:drawable="@drawable/check_false"></item>  
</selector>  

2、在layout中添加checkbox控件: 

<CheckBox  
    android:id="@+id/radioButton1"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:background="@drawable/checkbox_style"  
    android:button="@null"  
    android:checked="true"/>  

另外必须将android:button设置为@null。 
@drawable/check_true和@drawable/check_false为checkbox不同状态的图片,可自行设计。

推荐阅读