目錄介紹
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í)

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

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="登 錄"/>