Android設定Selector不同狀態下顏色及圖片
阿新 • • 發佈:2019-02-20
Selector常用狀態:
android:state_selected 控制元件選中狀態,可以為true或false
android:state_focused 控制元件獲得焦點狀態,可以為true或false
android:state_pressed 控制元件點選狀態,可以為true或false
android:state_enabled 控制元件使能狀態,可以為true或false
android:state_checkable 控制元件可勾選狀態,可以為true或false
android:state_checked 控制元件勾選狀態,可以為true或false
設定不同狀態下顏色:
btn_enabled.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 預設 -->
<item android:state_enabled="true">
<color android:color="#e25050" />
</item>
<item android:state_enabled="false">
<color android:color="#908e8e" />
</item>
</selector>
設定不同狀態下圖片:
btn_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/btn_arrow_left_pressed" />
<item android:state_window_focused="true" android:state_focused="true" android:drawable="@drawable/btn_arrow_left_pressed" />
<item android:state_window_focused="false" android:state_focused="false" android:drawable="@drawable/btn_arrow_left_normal" />
</selector>
android:state_selected 控制元件選中狀態,可以為true或false
android:state_focused 控制元件獲得焦點狀態,可以為true或false
android:state_pressed 控制元件點選狀態,可以為true或false
android:state_enabled 控制元件使能狀態,可以為true或false
android:state_checkable 控制元件可勾選狀態,可以為true或false
android:state_checked 控制元件勾選狀態,可以為true或false
設定不同狀態下顏色:
btn_enabled.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 預設 -->
<item android:state_enabled="true">
<color android:color="#e25050" />
</item>
<item android:state_enabled="false">
<color android:color="#908e8e" />
</item>
</selector>
設定不同狀態下圖片:
btn_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/btn_arrow_left_pressed" />
<item android:state_window_focused="true" android:state_focused="true" android:drawable="@drawable/btn_arrow_left_pressed" />
<item android:state_window_focused="false" android:state_focused="false" android:drawable="@drawable/btn_arrow_left_normal" />
</selector>