在android中,我们有时候考虑到界面的显示效果,就不会使用系统自带的样式。因项目需要,趁机学习一下:
这就是显示效果,前边为未选中状态,后边为选中状态。
现在在res/drawable下新建一个android xml文件 根节点选择selector ,然后 在里面添加两个item 用于设置不同的状态。
例如:<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@drawable/checked"></item>
<item android:state_checked="false" android:drawable="@drawable/unchecked"></item>
</selector>
然后将checkbox控件的background设置为以上的xml文件,并设置button属性为@null ,注意:必须设置button属性,不然会有原来的样式的。
例如: <CheckBox
android:id="@+id/cb_autoLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:background="@drawable/login_checkstatus" />
这样,你就可以根据自身需求来编辑代码啦。