android里面自定义RadioButton、CheckBox
正常情况下自定义RadioButton只需加一个style属性即可,但是如果需要后面追加文字的话,效果就不太如意了,如图所示
<RadioButton android:text="选项A"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="@style/radiobtn"
android:gravity="center_vertical"/>
<RadioButton android:text="选项B"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="@style/radiobtn"
android:gravity="center_vertical"/>
相比较下图效果,哪个是你想要的呢?
<RadioButton android:text="选项A"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:button="@null"
android:drawableLeft="@drawable/radiobtn_style"
android:background="@null"
android:gravity="center_vertical"/>
<RadioButton android:text="选项B"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:button="@null"
android:drawableLeft="@drawable/radiobtn_style"
android:background="@null"
android:gravity="center_vertical"/>
下面是style.xml文件和radiobtn_style.xml文件内容
style.xml内容:
<?xml version="1.0"encoding="utf-8"?>
<resources>
<style name="radiobtn">
<itemname="android:button">@drawable/radiobtn_style</item>
</style>
</resources>
radiobtn_style.xml文件内容:
<?xml version="1.0"encoding="utf-8"?>
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"android:drawable="@drawable/radio_true"></item>
<item android:state_checked="false"android:drawable="@drawable/radio_false"></item>
</selector>