android自定义checkBox样式

来源:互联网 发布:windows聚焦图片来源 编辑:程序博客网 时间:2024/05/19 13:59

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">大部分情况下,并不需要自定义控件,只需要设置一下checkBox的属性就可以了,</span>

第一种方法:

第一步:定义drawable样式文件


<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/checked" android:state_checked="true" />    <item android:drawable="@drawable/checked_false" android:state_checked="false" />    <item android:drawable="@drawable/checked" android:state_pressed="true" />    <item android:drawable="@drawable/checked_false" /></selector>

第二步:定义一个style

    <style name="checkBoxStyle">        <item name="android:button">@drawable/checkbox_style</item>    </style>

定义的是android:button属性,值是我们刚才定义的drawable


第三步:在xml配置文件中使用这个style

    <CheckBox        android:id="@+id/checkBox"        android:layout_width="25dp"        android:layout_height="25dp"        android:layout_gravity="center"        style="@style/checkBoxStyle"        android:layout_marginLeft="10dp" />



第二种方法:

第一步:定义drawable样式文件


<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/checked" android:state_checked="true" />    <item android:drawable="@drawable/checked_false" android:state_checked="false" />    <item android:drawable="@drawable/checked" android:state_pressed="true" />    <item android:drawable="@drawable/checked_false" /></selector>

第二步:在xml文件中配置

    <CheckBox        android:id="@+id/checkBox"        android:layout_width="25dp"        android:layout_height="25dp"        android:layout_gravity="center"        android:background="@drawable/checkbox_style"        android:button="@null"        android:layout_marginLeft="10dp" />

这种方法,不用设置style了,直接设置的是background属性,然后,把button属性设置为@null

0 0
原创粉丝点击