1. 程式人生 > >RadioGroup實現點選的RadioButton項變色

RadioGroup實現點選的RadioButton項變色

drawable下新建selector型別的檔案
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@color/colorSelect"/>//選中時顏色
    <item android:state_checked="false" android:drawable="@color/colorNoSelect"
/>//未選中時顏色 <item android:drawable="@color/colorNoSelect"/>//預設顏色 </selector> //佈局中RadioGroup程式碼,,,
在每個RadioButton中引用,剛才寫的檔案,android:background="@drawable/radio_selector"

//給預設展示頁面的對應的RadioButton中設定預設選中,android:cheched="true"
<RadioGroup
android:id="@+id/radiogroup"
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_alignParentBottom="true" android:layout_height="40dp"> <RadioButton android:id="@+id/home_button" android:background="@drawable/radio_selector" android:gravity="center" android:layout_width="0dp" android:button="@null" android:layout_weight="1"
android:layout_height="match_parent" android:text="首頁"/> <RadioButton android:id="@+id/zixuan_button" android:background="@drawable/radio_selector" android:gravity="center" android:layout_width="0dp" android:button="@null" android:layout_weight="1" android:layout_height="match_parent" android:text="自選"/> <RadioButton android:id="@+id/hangqing_button" android:background="@drawable/radio_selector" android:gravity="center" android:layout_width="0dp" android:button="@null" android:layout_weight="1" android:layout_height="match_parent" android:text="行情"/> <RadioButton android:id="@+id/zixun_button" android:background="@drawable/radio_selector" android:checked="true" android:gravity="center" android:layout_width="0dp" android:button="@null" android:layout_weight="1" android:layout_height="match_parent" android:text="資訊"/> <RadioButton android:id="@+id/jiaoyi" android:background="@drawable/radio_selector" android:gravity="center" android:layout_width="0dp" android:button="@null" android:layout_weight="1" android:layout_height="match_parent" android:text="交易"/> </RadioGroup>