1. 程式人生 > >在xml檔案中控制字型顏色隨著按鈕狀態改變

在xml檔案中控制字型顏色隨著按鈕狀態改變

1、建立on_blace_off_white_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="@color/black"
          android:drawable="@color/black"/>
    <!-- pressed -->
<item android:state_focused="true" android:color="@color/black" android:drawable="@color/black"/> <!-- focused --> <item android:state_checked="true" android:color="@color/black" android:drawable="@color/black"/> <!-- checked -->
<item android:color="@color/black" android:drawable="@color/white"/> <!-- default 此為預設的,一定要有--> </selector>

NOTE:其為一個selector,drawable如果不設定,編譯可能無法通過,如果要此selector要被設定為textColor,那麼color選項一定要有

2、在對應的按鈕中設定textColor為@drawable/on_blace_off_white_selector,如下:

<RadioButton
                android:layout_width="wrap_content"
android:layout_height="wrap_content" style="@style/home_page_tab_style" android:textColor="@drawable/on_black_off_white_selector" android:text="haha" />