自定义checkbox样式

来源:互联网 发布:java添加图片代码 编辑:程序博客网 时间:2024/06/07 10:21
创建selector_checkbox.xml文件内容如下
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_selected="true" android:drawable="@drawable/check_on"/><item android:state_checked="true" android:drawable="@drawable/check_on"/><item android:state_pressed="true" android:drawable="@drawable/check_on"/>    <item android:drawable="@drawable/check_off"/></selector>


在checkbox控件中调用selector_checkbox.xml文件

    <CheckBox         android:id="@+id/e_userstate"        android:layout_toRightOf="@+id/t_userstate"        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:layout_marginLeft="15dp"        android:layout_centerVertical="true"        android:background="@drawable/selector_checkbox"        android:button="@null"        />    <TextView         android:id="@+id/tip"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="20dp"        android:layout_centerVertical="true"        android:text="@string/user_enable"        android:layout_toRightOf="@+id/e_userstate"        />


注意:必须将android:button设置为@null。

后来发现可以直接设置android:button的背景也可以达到效果

<CheckBoxandroid:layout_width="wrap_content"        android:layout_height="wrap_content"        android:button="@drawable/selector_checkbox"        android:layout_centerVertical="true" android:layout_alignParentRight="true"        android:text="自动登录"         />






0 0
原创粉丝点击