国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android selector的實例詳解

2019-12-12 02:13:48
字體:
來源:轉載
供稿:網友

Android  selector的詳解

前言:

StateListDrawable 是一種通過XML文件來定義的drawable,使用幾個不同的圖片來呈現同一個圖形,通過object的狀態來實現切換。例如,一個Button有幾個不同的狀態(按壓,獲取焦點等等),這種情況下,通過使用 state list drawable,你就可以實現在不同的狀態下使用不同的背景圖片。

你可以在一個XML文件中描述state list。通過在根節點selector下定義一個item元素來添加每個圖形。每一各item中使用不同的狀態屬性來定義不用的drawable。

當每一次狀態改變的時候,state list都會從上到下被遍歷一遍,第一個與當前state相匹配的item將會被使用―- 這個選擇并不是作出“最匹配”結果,而是簡單的找到第一個匹配的狀態。

selector一般都是用來作為有狀態改變的View的背景,以此來達到當用戶對View進行操作,導致View狀態改變時,作出改變,讓用戶感知View的狀態變化。

官方說明

文件位置: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>

更多詳細說明,請查閱xsoftlab

實際使用

下面做一個簡單的實例,對Button的背景根據狀態做一下處理

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>

代碼部分 比較簡單,這里就不占用過多的篇幅了,看下簡單的效果,大家應該就知道如何編寫小小的Activity了。

實際效果

如有疑問請留言或者到本站社區交流討論,本站關于Android 開發的文章還有很多,歡迎大家搜索查閱,謝謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 白沙| 瑞安市| 雷山县| 安平县| 汉源县| 蓬莱市| 宁安市| 定安县| 本溪市| 察雅县| 平安县| 梓潼县| 衡山县| 德钦县| 西城区| 鸡西市| 城步| 临夏县| 泾阳县| 汶上县| 安庆市| 略阳县| 郯城县| 阿巴嘎旗| 栾城县| 梅河口市| 宁夏| 大方县| 绍兴县| 施秉县| 万源市| 江孜县| 绥德县| 西峡县| 五指山市| 石楼县| 永城市| 东源县| 塘沽区| 庆元县| 安庆市|