Android Drawable Resource學習(五)、StateListDrawable

來源:互聯網
上載者:User

一個StateListDrawable就是一個在xml檔案中定義,根據該對象不同的狀態,用幾張不同的圖片來代表相同的圖形。比如,一個按鈕,有多種狀態,擷取焦點,失去焦點,點擊等等,使用StateListDrawable可以根據不同的狀態提供不同的背景。

在XML檔案中描述這些狀態列表。在唯一的一個<selector>標籤下,使用<item>標籤來代表一個圖形。每個<item>標籤使用各種屬性來描述它所代表的狀態所需要的drawable資源。

再次狀態發生改變的時候,都會從上到下遍曆這個狀態列表,第一個和它匹配的將會被使用-------而不是去選擇最適合的匹配。

檔案位置

res/drawable/filename.xmlThe filename is used as the resource ID.

編譯資料類型:

指向StateListDrawable的指標

資源引用:

In Java: R.drawable.filenameIn 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>

元素:

              <selector>:必須的,必須最為根項目,包含一個或多個<item>元素

屬性

xmlns:android
字串。 必須的。定義命名空間,必須是 "http://schemas.android.com/apk/res/android"
android:constantSize
布爾值。內部大小(所有狀態中最大的那個)改變時是否報道。預設為false
android:dither
布爾值。如果位元影像與螢幕的像素配置不同時,是否允許抖動.(例如:一個位元影像的像素設定是 ARGB 8888,但螢幕的設定是RGB 565)
android:variablePadding
布爾值。預設為false,是否要進行繪圖填充。(不明白)
<item>
為某個狀態定義一個drawable,必須作為<selector>的子項目。

屬性:

android:drawable
必須的,指向一個drawable資源
android:state_pressed
Boolean。是否按下
android:state_focused
Boolean。是否獲得獲得焦點
android:state_hovered
Boolean。滑鼠在上面滑動的狀態。通常和state_focused使用同樣的drawable

api14後新增的

android:state_selected
Boolean。是否選中
android:state_checkable
Boolean。是否可以被勾選(checkable)。只能用在可以勾選的控制項上
android:state_checked
Boolean。是否被勾選上
android:state_enabled
Boolean。是否可用
android:state_activated
Boolean。是否被啟用並持久的選擇

api11後新增

android:state_window_focused
Boolean。當前應用程式是否獲得焦點

注意:Android系統將會選中第一條符合目前狀態的item。。因此,如果第一項列表中包含了所有的狀態屬性,那麼它是每次就只用他。這就是為什麼你的預設值應該放在最後面。

舉例:

XML檔案的實現:

<?xml version="1.0" encoding="UTF-8"?><selector  xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_focused="true" android:drawable="@drawable/botton_add" />    <item android:state_pressed="true" android:drawable="@drawable/botton_add_down" />    <item android:state_selected="true" android:drawable="@drawable/botton_add" />    <item android:drawable="@drawable/botton_add" />//預設</selector>
<Button         android:background="@drawable/statelist"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>

Java代碼的實現:

 Button btn=(Button)findViewById(R.id.btn);        StateListDrawable drawable=new StateListDrawable();        //如果要設定莫項為false,在前面加負號 ,比如android.R.attr.state_focesed標誌true,-android.R.attr.state_focesed就標誌false        drawable.addState(new int[]{android.R.attr.state_focused}, this.getResources().getDrawable(R.drawable.botton_add));        drawable.addState(new int[]{android.R.attr.state_pressed}, this.getResources().getDrawable(R.drawable.botton_add_down));        drawable.addState(new int[]{android.R.attr.state_selected}, this.getResources().getDrawable(R.drawable.botton_add));        drawable.addState(new int[]{}, this.getResources().getDrawable(R.drawable.botton_add));//預設        btn.setBackgroundDrawable(drawable);
<Button        android:id="@+id/btn"         android:layout_width="wrap_content"        android:layout_height="wrap_content"/>

使用舉例:

http://www.apkbus.com/forum.php?mod=viewthread&tid=52722

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.