Android 自定义CheckBox样式

来源:互联网 发布:centos 7 route add 编辑:程序博客网 时间:2024/05/19 13:43

尊重原创成果,转载注明出处!

原文地址:http://wsy1983wsy.iteye.com/blog/1471573

1、首先res/drawable中定义编写如下样式: 

<?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/btn_check"
            android:button="@null"
            android:checked="true"/>

如果CheckBox除了按钮还有图片的话就要按下面的代码设置checkBox了!

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

其中drwable/btn_check为1中顶一个文件名称,另外必须将android:button设置为@null。 
@drawable/check_true和@drawable/check_false为checkbox不同状态的图片,可自行设计。

0 0
原创粉丝点击