读书人

更动CheckBox的样式

发布时间: 2012-09-25 09:55:59 作者: rapoo

更改CheckBox的样式

首先在styles.xml添加如下的自定义样式:

<style name="mycheckbox" parent="@android:style/Widget.CompoundButton.CheckBox"><item name="android:button">@drawable/my_checkbox</item></style>

?my_checkbox.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_pressed" />                                                                                           <item android:state_checked="false" android:drawable="@drawable/checkbox" />     <item android:drawable="@drawable/checkbox" />                                                                                                </selector>

?

使用自定义样式的代码段:

<CheckBox   android:id="@+id/checked"    android:layout_alignParentRight="true"   android:layout_marginRight="10dip"   android:layout_centerVertical="true"   android:layout_width="40dip"    android:layout_height="wrap_content"   android:checked="false"   style="@style/mycheckbox"    />

?添加两张图片checkbox_pressed.png、checkbox.png

读书人网 >移动开发

热点推荐