一般自帶的單選框比較難看,有潔癖的大家估計不好接受,下面謝謝自定義的單選框。
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: