Android 02 selector狀態(tài)選擇器

目錄介紹
1.selector簡單介紹
2.selector創(chuàng)建方式
3.在Xml中標(biāo)簽說明
4.狀態(tài)設(shè)置說明
5.舉例子,在按鈕的背景中
6.舉例子,用于按鈕的文本顏色
7.注意和了解
8.遇到問題:TextView如何設(shè)置背景和字體選擇器?

1.selector簡單介紹
實(shí)際應(yīng)用中,很多地方比如按鈕Button、Tab、ListItem等都是不同狀態(tài)有不同的展示形狀。
舉個(gè)例子,一個(gè)按鈕的背景,默認(rèn)時(shí)是一個(gè)形狀,按下時(shí)是一個(gè)形狀,不可操作時(shí)又是另一個(gè)形狀。
有時(shí)候,不同狀態(tài)下改變的不只是背景、圖片等,文字顏色也會相應(yīng)改變。而要處理這些不同狀態(tài)下展示什么的問題,就要用selector來實(shí)現(xiàn)。

2.selector創(chuàng)建方式
第一種:在Xml中直接創(chuàng)建selector的Xml文件,容易掌握,簡單但是不靈活
第二種:在代碼中動態(tài)創(chuàng)建selector,較為復(fù)雜,但是靈活,應(yīng)用場景是選擇器狀態(tài)是不斷變化的

3.在Xml中標(biāo)簽說明
selector標(biāo)簽,可以添加一個(gè)或多個(gè)item子標(biāo)簽,而相應(yīng)的狀態(tài)是在item標(biāo)簽中定義的。
定義的xml文件可以作為兩種資源使用:drawable和color。

1.作為drawable資源使用時(shí),一般和shape一樣放于drawable目錄下,item必須指定android:drawable屬性。
2.作為color資源使用時(shí),則放于color目錄下,item必須指定android:color屬性。

4.狀態(tài)設(shè)置說明

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會影響觸摸或點(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),例如拉下通知欄或彈出對話框時(shí), 當(dāng)前界面就會失去焦點(diǎn);另外,ListView的ListItem獲得焦點(diǎn)時(shí)也會觸發(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)在上面滑動的狀態(tài)**,true表示鼠標(biāo)在上面滑動,默認(rèn)為false,API Level 14及以上才支持
補(bǔ)充:selector標(biāo)簽下有兩個(gè)比較有用的屬性要說一下,添加了下面兩個(gè)屬性之后,則會在狀態(tài)改變時(shí)出現(xiàn)淡入淡出效果,
但必須在API Level 11及以上才支持
android:exitFadeDuration             **狀態(tài)改變時(shí),舊狀態(tài)消失時(shí)的淡出時(shí)間,以毫秒為單位**
android:enterFadeDuration            **狀態(tài)改變時(shí),新狀態(tài)展示時(shí)的淡入時(shí)間,以毫秒為單位**

5.舉例子,在按鈕的背景中

<?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>

6.舉例子,用于按鈕的文本顏色

<?xml version="1.0" encoding="utf-8"?>
<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>

7.注意和了解
selector作為drawable資源時(shí),item指定android:drawable屬性,并放于drawable目錄下;
selector作為color資源時(shí),item指定android:color屬性,并放于color目錄下;
color資源也可以放于drawable目錄,引用時(shí)則用@drawable來引用,但不推薦這么做,drawable資源和color資源最好還是分開;
android:drawable屬性除了引用@drawable資源,也可以引用@color顏色值;但android:color只能引用@color;
item是從上往下匹配的,如果匹配到一個(gè)item那它就將采用這個(gè)item,而不是采用最佳匹配的規(guī)則;所以設(shè)置默認(rèn)的狀態(tài),

注意:一定要寫在最后,如果寫在前面,則后面所有的item都不會起作用了。

8.關(guān)于ListView的ListItem樣式,設(shè)置方式
一種是在ListView標(biāo)簽里設(shè)置android:listSelector屬性
另一種是在ListItem的布局layout里設(shè)置android:background。

但是,這兩種設(shè)置的結(jié)果卻有著不同。同時(shí),使用ListView時(shí)也有些其他需要注意的地方,總結(jié)如下:
1.android:listSelector設(shè)置的ListItem默認(rèn)背景是透明的,不管你在selector里怎么設(shè)置都無法改變它的背景。
2.所以,如果想改ListItem的默認(rèn)背景,只能通過第二種方式,在ListItem的布局layout里設(shè)置android:background。
3.當(dāng)觸摸點(diǎn)擊ListItem時(shí),第一種設(shè)置方式下,state_pressed、state_focused和state_window_focused設(shè)為true時(shí)都會觸發(fā),而第二種設(shè)置方式下,只有state_pressed會觸發(fā)。
4.當(dāng)ListItem里有Button或CheckBox之類的控件時(shí),會搶占ListItem本身的焦點(diǎn),導(dǎo)致ListItem本身的觸摸點(diǎn)擊事件會無效。要解決此問題,有三種解決方案:
將Button或CheckBox換成TextView或ImageView之類的控件
設(shè)置Button或CheckBox之類的控件設(shè)置focusable屬性為false
設(shè)置ListItem的根布局屬性android:descendantFocusability="blocksDescendants"

第三種是最方便,也是推薦的方式,它會將ListItem根布局下的所有子控件都設(shè)置為不能獲取焦點(diǎn)。android:descendantFocusability屬性的值有三種,其中,ViewGroup是指設(shè)置該屬性的View,本例中就是ListItem的根布局:

o    beforeDescendants:ViewGroup會優(yōu)先其子類控件而獲取到焦點(diǎn)
o    afterDescendants:ViewGroup只有當(dāng)其子類控件不需要獲取焦點(diǎn)時(shí)才獲取焦點(diǎn)
o    blocksDescendants:ViewGroup會覆蓋子類控件而直接獲得焦點(diǎn)

8.遇到問題:TextView如何設(shè)置背景和字體選擇器?
1.先看看需求
沒有點(diǎn)擊時(shí)

Image.png

點(diǎn)擊時(shí)

Image.png

2.遇到問題
點(diǎn)擊后背景色無變化
點(diǎn)擊后字體顏色無變化
問題代碼

<TextView
    android:id="@+id/tv_login"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_marginRight="40dp"
    android:layout_marginLeft="40dp"
    android:layout_marginTop="15dp"
    android:background="@drawable/selector_login_btn"
    android:gravity="center"
    **android:textColor="@drawable/selector_login_text_color"**
    android:textSize="16sp"
    android:text="登 錄"/>

3.解決辦法
注意,對android:textColor賦值時(shí),不是@drawable/xxxxx,而是@color/XXXXX

<TextView
    android:id="@+id/tv_login"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_marginRight="40dp"
    android:layout_marginLeft="40dp"
    android:layout_marginTop="15dp"
    android:background="@drawable/selector_login_btn"
    **android:clickable="true"**
    android:gravity="center"
    **android:textColor="@color/selector_login_text_color"**
    android:textSize="16sp"
    android:text="登 錄"/>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,155評論 25 708
  • 概述 今天我們來探究一下android的樣式。其實(shí),幾乎所有的控件都可以使用 background屬性去引用自定義...
    CokeNello閱讀 5,117評論 1 19
  • 轉(zhuǎn)自Keegan小鋼 shape只能定義單一的形狀,而實(shí)際應(yīng)用中,很多地方比如按鈕、Tab、ListItem等都是...
    MrMagicWang閱讀 2,400評論 0 7
  • 你為他流的眼淚 像刀子 戳進(jìn)我心里 割斷了我最后一絲勇氣 說想你 我沉默地望著 窗外 深夜的雨 正劃傷空氣 擁吻大地
    我叫王村村閱讀 780評論 2 12
  • 小長假第一天,主要任務(wù)陪孩子,利用孩子睡覺間歇收拾衣柜,整理換季衣服鞋子。 說是整理,我把這次的基調(diào)定的很明確,就...
    有魚上上簽閱讀 248評論 0 1

友情鏈接更多精彩內(nèi)容