Android selector的實(shí)例詳解
Android selector的詳解
前言:
StateListDrawable 是一種通過(guò)XML文件來(lái)定義的drawable,使用幾個(gè)不同的圖片來(lái)呈現(xiàn)同一個(gè)圖形,通過(guò)object的狀態(tài)來(lái)實(shí)現(xiàn)切換。例如,一個(gè)Button有幾個(gè)不同的狀態(tài)(按壓,獲取焦點(diǎn)等等),這種情況下,通過(guò)使用 state list drawable,你就可以實(shí)現(xiàn)在不同的狀態(tài)下使用不同的背景圖片。
你可以在一個(gè)XML文件中描述state list。通過(guò)在根節(jié)點(diǎn)selector下定義一個(gè)item元素來(lái)添加每個(gè)圖形。每一各item中使用不同的狀態(tài)屬性來(lái)定義不用的drawable。
當(dāng)每一次狀態(tài)改變的時(shí)候,state list都會(huì)從上到下被遍歷一遍,第一個(gè)與當(dāng)前state相匹配的item將會(huì)被使用—- 這個(gè)選擇并不是作出“最匹配”結(jié)果,而是簡(jiǎn)單的找到第一個(gè)匹配的狀態(tài)。
selector一般都是用來(lái)作為有狀態(tài)改變的View的背景,以此來(lái)達(dá)到當(dāng)用戶(hù)對(duì)View進(jìn)行操作,導(dǎo)致View狀態(tài)改變時(shí),作出改變,讓用戶(hù)感知View的狀態(tài)變化。
官方說(shuō)明
文件位置:res/drawable/filename.xml
編譯資源類(lèi)型:StateListDrawable
資源引用:
In Java: R.drawable.filename
In XML: @[package:]drawable/filename
語(yǔ)法:
<?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ì)說(shuō)明,請(qǐng)查閱xsoftlab
實(shí)際使用
下面做一個(gè)簡(jiǎn)單的實(shí)例,對(duì)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>
代碼部分 比較簡(jiǎn)單,這里就不占用過(guò)多的篇幅了,看下簡(jiǎn)單的效果,大家應(yīng)該就知道如何編寫(xiě)小小的Activity了。
實(shí)際效果

如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,本站關(guān)于Android 開(kāi)發(fā)的文章還有很多,歡迎大家搜索查閱,謝謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Android如何從實(shí)現(xiàn)到封裝一個(gè)MVP詳解
原生的 MVC 框架遇到大規(guī)模的應(yīng)用,就會(huì)變得代碼難讀,不好維護(hù),無(wú)法測(cè)試的囧境。因此,Android 開(kāi)發(fā)方面也有很多對(duì)應(yīng)的框架來(lái)解決這些問(wèn)題。所以這篇文章主要給大家介紹了關(guān)于Android如何從實(shí)現(xiàn)到封裝一個(gè)MVP的相關(guān)資料,需要的朋友可以參考下。2017-09-09
Android仿抖音右滑清屏左滑列表功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android仿抖音右滑清屏左滑列表功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Android6.0 Launcher2應(yīng)用解析
這篇文章主要為大家詳細(xì)介紹了Android6.0 Launcher2應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Android Scroll滑動(dòng)效果實(shí)例
這篇文章主要為大家分享了Android Scroll滑動(dòng)效果實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04
Android學(xué)習(xí)之BottomSheetDialog組件的使用
BottomSheetDialog是底部操作控件,可在屏幕底部創(chuàng)建一個(gè)支持滑動(dòng)關(guān)閉視圖。本文將通過(guò)示例詳細(xì)講解它的使用,感興趣的小伙伴可以了解一下2022-06-06
解析Android開(kāi)發(fā)優(yōu)化之:從代碼角度進(jìn)行優(yōu)化的技巧
下面我們就從幾個(gè)方面來(lái)了解Android開(kāi)發(fā)過(guò)程中的代碼優(yōu)化,需要的朋友參考下2013-05-05
Android開(kāi)發(fā)之在xml中設(shè)置自定義屬性的方法
下面小編就為大家分享一篇Android開(kāi)發(fā)之在xml中設(shè)置自定義屬性的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01

