Android selector的實例詳解
Android selector的詳解
前言:
StateListDrawable 是一種通過XML文件來定義的drawable,使用幾個不同的圖片來呈現(xiàn)同一個圖形,通過object的狀態(tài)來實現(xiàn)切換。例如,一個Button有幾個不同的狀態(tài)(按壓,獲取焦點等等),這種情況下,通過使用 state list drawable,你就可以實現(xiàn)在不同的狀態(tài)下使用不同的背景圖片。
你可以在一個XML文件中描述state list。通過在根節(jié)點selector下定義一個item元素來添加每個圖形。每一各item中使用不同的狀態(tài)屬性來定義不用的drawable。
當(dāng)每一次狀態(tài)改變的時候,state list都會從上到下被遍歷一遍,第一個與當(dāng)前state相匹配的item將會被使用—- 這個選擇并不是作出“最匹配”結(jié)果,而是簡單的找到第一個匹配的狀態(tài)。
selector一般都是用來作為有狀態(tài)改變的View的背景,以此來達(dá)到當(dāng)用戶對View進(jìn)行操作,導(dǎo)致View狀態(tài)改變時,作出改變,讓用戶感知View的狀態(tài)變化。
官方說明
文件位置:res/drawable/filename.xml
編譯資源類型:StateListDrawable
資源引用:
In Java: R.drawable.filename
In XML: @[package:]drawable/filename
語法:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize=["true" | "false"] android:dither=["true" | "false"] android:variablePadding=["true" | "false"] > <item android:drawable="@[package:]drawable/drawable_resource" android:state_pressed=["true" | "false"] android:state_focused=["true" | "false"] android:state_hovered=["true" | "false"] android:state_selected=["true" | "false"] android:state_checkable=["true" | "false"] android:state_checked=["true" | "false"] android:state_enabled=["true" | "false"] android:state_activated=["true" | "false"] android:state_window_focused=["true" | "false"] /> </selector>
更多詳細(xì)說明,請查閱xsoftlab
實際使用
下面做一個簡單的實例,對Button的背景根據(jù)狀態(tài)做一下處理
XML文件
selector_ts.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/pink" android:state_pressed="true" /> <item android:drawable="@color/yellow" android:state_selected="true" /> <item android:drawable="@drawable/shaperect" android:state_enabled="false" /> <item android:drawable="@color/stone" android:state_enabled="true" /> </selector>
主布局文件(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:focusable="true" android:focusableInTouchMode="true" android:orientation="vertical" android:padding="10dp" tools:context="mraz.com.tabdemo.MainActivity"> <Button android:id="@+id/bt_content" android:layout_width="match_parent" android:layout_height="300dp" android:background="@drawable/selector_ts" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:orientation="horizontal"> <Button android:id="@+id/bt_selected" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Select" android:textAllCaps="false" /> <Button android:id="@+id/bt_disable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Disable" android:textAllCaps="false" /> <Button android:id="@+id/bt_pressed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Press" android:textAllCaps="false" /> </LinearLayout> </LinearLayout>
代碼部分 比較簡單,這里就不占用過多的篇幅了,看下簡單的效果,大家應(yīng)該就知道如何編寫小小的Activity了。
實際效果
如有疑問請留言或者到本站社區(qū)交流討論,本站關(guān)于Android 開發(fā)的文章還有很多,歡迎大家搜索查閱,謝謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
ScrollView與SeekBar綁定實現(xiàn)滑動時出現(xiàn)小滑塊效果
這篇文章主要為大家詳細(xì)介紹了ScrollView與SeekBar綁定實現(xiàn)滑動時出現(xiàn)小滑塊效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10Android HttpURLConnection斷點下載(單線程)
這篇文章主要為大家詳細(xì)介紹了Android HttpURLConnection斷點下載的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05Kotlin中l(wèi)et()with()run()apply()also()函數(shù)的使用方法與區(qū)別
在Kotlin中的源碼標(biāo)準(zhǔn)庫(Standard.kt)中提供了一些Kotlin擴(kuò)展的內(nèi)置函數(shù)可以優(yōu)化kotlin的編碼,今天為大家聊聊let,with,run,apply,also幾個函數(shù)的用法與區(qū)別2018-03-03談?wù)剬ndroid View事件分發(fā)機(jī)制的理解
本篇文章主要介紹了談?wù)剬ndroid View事件分發(fā)機(jī)制的理解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01Android DrawerLayout實現(xiàn)抽屜效果實例代碼
這篇文章主要介紹了Android DrawerLayout實現(xiàn)抽屜效果的實例代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-12-12Android 后臺發(fā)送郵件示例 (收集應(yīng)用異常信息+Demo代碼)
今天介紹個更簡單的方法,我們把異常信息收集后,通過后臺發(fā)送郵件方法,把相關(guān)異常信息發(fā)送到我們指定的郵箱里面2013-07-07微前端架構(gòu)ModuleFederationPlugin源碼解析
這篇文章主要為大家介紹了微前端架構(gòu)ModuleFederationPlugin源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Flutter Reusable Lottie Animations技巧
這篇文章主要為大家介紹了Flutter Reusable Lottie Animations技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Android通過Java sdk的方式接入OpenCv的方法
這篇文章主要介紹了Android通過Java sdk的方式接入OpenCv的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04