UI-CheckBox

CheckBox

一个ImageView实现CheckBox功能

定义ImageView布局控件

1
2
3
4
5
6
7
8
9
10
<ImageView
android:id="@+id/iv_select"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:scaleType="centerCrop"
android:src="@drawable/draftboxselector"/>

其中src的资源为(drawable下的draftboxselector.xml

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/ic_caogao_unselect"
android:maxLevel="0" />
<item
android:drawable="@drawable/ic_caogao_select"
android:maxLevel="1" />
</level-list>

调用

1
2
3
selectIv.setImageLevel(1);

selectIv.setImageLevel(0);

checkbox样式

  1. 首先要导入你准备用作CheckBox选中和补选中状态的两图片到res的drawable中,如checkbox_checked.png,checkbox_normal.png;

在res/drawable中添加checkbox.xml,定义checkbox的state list drawable图片

1
2
3
4
5
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/checkbox_checked" /> <!-- checked -->
<item android:state_checked="false" android:drawable="@drawable/checkbox_normal" /> <!-- default -->
</selector>
  1. 在Layout中修改checkbox的属性:android:button=”@drawable/checkbox” 定制button样式
1
<CheckBox android:layout_height="wrap_content" android:id="@+id/chkItem" android:button="@drawable/checkbox"></CheckBox>

这样就完成了定制工作,效果如下:
img