1. 程式人生 > >Android RadioButton自定義背景色圓角樣式的方法

Android RadioButton自定義背景色圓角樣式的方法

先看圖,實現如下樣式:
這裡寫圖片描述
來看該radiobutton的程式碼:

 <RadioButton
            android:id="@+id/rb_a"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="30dip"
            android:text="A"
            android:checked="true"
            android:button="@null
"
android:gravity="center" android:textColor="@drawable/comment_stu_tv_bg" android:background="@drawable/comment_stu_bg" />

comment_stu_tv_bg.xml的程式碼如下

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
>
<item android:state_checked="true" android:color="@color/white"/> <!-- not selected --> <item android:state_checked="false" android:color="@color/black"/> </selector>

comment_stu_bg.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/radiobutton_background_checked" > </item> <item android:state_checked="false" android:drawable="@drawable/radiobutton_background_unchecked"> </item> </selector>

radiobutton_background_checked.xml的程式碼如下:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <!-- 填充 -->
    <solid android:color="@color/orange_main_color" />
    <!-- 圓角 -->
    <corners android:radius="5dp" />
</shape>

radiobutton_background_unchecked.xml的程式碼如下:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <!-- 填充 -->
    <solid android:color="@color/gray_radio_bg" />
    <!-- 圓角 -->
    <corners android:radius="5dp" />
</shape>