一般自带的单选框比较难看,有洁癖的大家估计不好接受,下面谢谢自定义的单选框。
1)与Button点击变色的道理一样,首先要建一个selector资源选择器文件如下
文件名称:sele.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_enabled="true"
android:state_checked="true"
android:drawable="@drawable/select"/>
<item
android:state_enabled="true"
android:state_checked="false"
android:drawable="@drawable/unselect"/>
</selector>
2)在style中添加样式name=”MyRadioButton”
<style name="MyRadioButton">
<item name="android:button">@drawable/sele</item>
<item name="android:textColor">@color/material_blue_grey_800</item>
</style>
3)在布局文件中引用样式
<RadioButton
android:id="@+id/btn_man"
style="@style/MyRadioButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:checked="true"
android:text="男" />
这样就可以啦,不过值得注意的是,有时候你会发现选择按钮没有显示全,还有评分条ratingbar也会出现这种情况,好像适配上有问题似的,这个时候有一个办法就是把图片文件在每一个图片文件夹里都放置一套,这个问题就解决啦。如图:
这里可以下demo: