<Android 基礎(十三)> selector

來源:互聯網
上載者:User

標籤:

介紹

A StateListDrawable is a drawable object defined in XML that uses a several different images to represent the same graphic, depending on the state of the object. For example, a Button widget can exist in one of several different states (pressed, focused, or niether) and, using a state list drawable, you can provide a different background image for each state.

You can describe the state list in an XML file. Each graphic is represented by an “item” element inside a single “selector” element. Each “item” uses various attributes to describe the state in which it should be used as the graphic for the drawable.

During each state change, the state list is traversed top to bottom and the first item that matches the current state is used—the selection is not based on the “best match,” but simply the first item that meets the minimum criteria of the state.
翻譯:
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 基礎(十三)> selector

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.