android中自定义checkbox的图片和大小

来源:互联网 发布:淘宝购买记录生成器 编辑:程序博客网 时间:2024/06/06 02:05

其实很简单,分三步:

1.在drawable中创建文件checkbox_selector.xml:

<?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_ok" /><!--设置选中图片--><item android:state_checked="false"  android:drawable="@drawable/checkbox_empty" /><!--设置未选中图片--></selector>

2.在values中创建styles.xml:

<?xml version="1.0" encoding="utf-8"?><resources>  <style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">   <item name="android:button">@drawable/checkbox_selector</item>   <item name="android:paddingLeft">25.0dip</item>   <item name="android:maxHeight">10.0dip</item>  </style></resources>

3.在你的CheckBox中添加属性:

<CheckBox        android:id="@+id/check"        android:layout_width="20dp"        android:layout_height="20dp"        android:layout_marginLeft="5dp"        style="@style/MyCheckBox"          />

搞定!这样就把你的checkbox换成你设置的那两张图片了