selector狀態(tài)器 - 可以添加一個(gè)或多個(gè)item子標(biāo)簽
- 作為drawable資源:放于drawable目錄下,item必須指定android:drawable屬性
- 作為color資源:放于color目錄下,item必須指定android:color屬性
狀態(tài)
android:state_enabled: 設(shè)置觸摸或點(diǎn)擊事件是否可用狀態(tài),一般只在false時(shí)設(shè)置該屬性,表示不可用狀態(tài)
android:state_pressed: 設(shè)置是否按壓狀態(tài),一般在true時(shí)設(shè)置該屬性,表示已按壓狀態(tài),默認(rèn)為false
android:state_selected: 設(shè)置是否選中狀態(tài),true表示已選中,false表示未選中
android:state_checked: 設(shè)置是否勾選狀態(tài),主要用于CheckBox和RadioButton,true表示已被勾選,false表示未被勾選
android:state_checkable: 設(shè)置勾選是否可用狀態(tài),類似state_enabled,只是state_enabled會(huì)影響觸摸或點(diǎn)擊事件,而state_checkable影響勾選事件
android:state_focused: 設(shè)置是否獲得焦點(diǎn)狀態(tài),true表示獲得焦點(diǎn),默認(rèn)為false,表示未獲得焦點(diǎn)
android:state_window_focused: 設(shè)置當(dāng)前窗口是否獲得焦點(diǎn)狀態(tài),true表示獲得焦點(diǎn),false表示未獲得焦點(diǎn),例如拉下通知欄或彈出對(duì)話框時(shí),當(dāng)前界面就會(huì)失去焦點(diǎn);另外,ListView的ListItem獲得焦點(diǎn)時(shí)也會(huì)觸發(fā)true狀態(tài),可以理解為當(dāng)前窗口就是ListItem本身
android:state_activated: 設(shè)置是否被激活狀態(tài),true表示被激活,false表示未激活,API Level 11及以上才支持,可通過代碼調(diào)用控件的setActivated(boolean)方法設(shè)置是否激活該控件
android:state_hovered: 設(shè)置是否鼠標(biāo)在上面滑動(dòng)的狀態(tài),true表示鼠標(biāo)在上面滑動(dòng),默認(rèn)為false,API Level 14及以上才支持
作為狀態(tài)器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 當(dāng)前窗口失去焦點(diǎn)時(shí) -->
<item android:drawable="@drawable/bg_btn_lost_window_focused" android:state_window_focused="false" />
<!-- 不可用時(shí) -->
<item android:drawable="@drawable/bg_btn_disable" android:state_enabled="false" />
<!-- 按壓時(shí) -->
<item android:drawable="@drawable/bg_btn_pressed" android:state_pressed="true" />
<!-- 被選中時(shí) -->
<item android:drawable="@drawable/bg_btn_selected" android:state_selected="true" />
<!-- 被激活時(shí) -->
<item android:drawable="@drawable/bg_btn_activated" android:state_activated="true" />
<!-- 默認(rèn)時(shí) -->
<item android:drawable="@drawable/bg_btn_normal" />
</selector>
作為color資源
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 當(dāng)前窗口失去焦點(diǎn)時(shí) -->
<item android:color="@android:color/black" android:state_window_focused="false" />
<!-- 不可用時(shí) -->
<item android:color="@android:color/background_light" android:state_enabled="false" />
<!-- 按壓時(shí) -->
<item android:color="@android:color/holo_blue_light" android:state_pressed="true" />
<!-- 被選中時(shí) -->
<item android:color="@android:color/holo_green_dark" android:state_selected="true" />
<!-- 被激活時(shí) -->
<item android:color="@android:color/holo_green_light" android:state_activated="true" />
<!-- 默認(rèn)時(shí) -->
<item android:color="@android:color/white" />
</selector>
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。