本篇內容主要講解“Android中的selector怎么使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Android中的selector怎么使用”吧!
鳳城ssl適用于網站、小程序/APP、API接口等需要進行數據傳輸應用場景,ssl證書未來市場廣闊!成為成都創新互聯的ssl證書銷售渠道,可以享受市場價格4-6折優惠!如果有意向歡迎電話聯系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
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>
到此,相信大家對“Android中的selector怎么使用”有了更深的了解,不妨來實際操作一番吧!這里是創新互聯網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
分享名稱:Android中的selector怎么使用
URL鏈接:http://www.2m8n56k.cn/article36/jojepg.html
成都網站建設公司_創新互聯,為您提供外貿建站、電子商務、動態網站、響應式網站、品牌網站建設、網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:[email protected]。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯