Android之drawable state各個屬性詳解


我們在定義一個drawable的時候可以通過xml定義的drawable對象。它使得一個圖片能在不同的狀態下顯示不同的圖案,比如一個Button,它有pressed,focused,或者其它狀態,通過使用state list drawable,你就可以為每種狀態提供不同的圖片。

先看一個范例:

 

XML file saved at res/drawable/button.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:state_enabled="true" android:state_window_focused="false"
          android:drawable="@drawable/button_pressed" /> <!-- pressed,enable等多個屬性 -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

his layout XML applies the state list drawable to a Button:

<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/button" />

ndroid:drawable 放一個drawable資源
android:state_pressed 是否按下,如一個按鈕觸摸或者點擊。
android:state_focused 是否取得焦點,比如用戶選擇了一個文本框。
android:state_hovered 光標是否懸停,通常與focused state相同,它是4.0的新特性
android:state_selected 被選中,它與focus state並不完全一樣,如一個list view 被選中的時候,它里面的各個子組件可能通過方向鍵,被選中了。
android:state_checkable 組件是否能被check。如:RadioButton是可以被check的。
android:state_checked 被checked了,如:一個RadioButton可以被check了。
android:state_enabled 能夠接受觸摸或者點擊事件
android:state_activated 被激活(例子)
android:state_window_focused 應用程序是否在前台,當有通知欄被拉下來或者一個對話框彈出的時候應用程序就不在前台了

 

注意:如果有多個item,那么程序將自動從上到下進行匹配,最先匹配的將得到應用。(不是通過最佳匹配)
如果一個item沒有任何的狀態說明,那么它將可以被任何一個狀態匹配。

 

詳細的請看官方的API,那里寫的更詳細~!http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM