2012年4月12日 星期四

了解Android resource檔案 (二)

接續上一個單元了解Android resource檔案 (一)

如果去追一下textAppearanceSmall可以找到

    <style name="TextAppearance.Small">
        <item name="android:textSize">14sp</item>
        <item name="android:textColor">?textColorSecondary</item>
    </style>

這說明textAppearanceSmall這個設定同時設定了字體大小以及顏色。我們有興趣的地方在顏色的設定,所以追蹤一下顏色可以發現

<item name="textColorSecondary">@android:color/secondary_text_dark</item>




以及在....res\color\目錄下的secondary_text_dark.xml檔案





<?xml version="1.0" encoding="utf-8"?>


            .......


<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="false" android:color="@android:color/dim_foreground_dark_disabled"/>
    <item android:state_window_focused="false" android:color="@android:color/dim_foreground_dark"/>
    <item android:state_selected="true" android:state_enabled="false" android:color="@android:color/dim_foreground_dark_inverse_disabled"/>
    <item android:state_pressed="true" android:state_enabled="false" android:color="@android:color/dim_foreground_dark_inverse_disabled"/>
    <item android:state_selected="true" android:color="@android:color/dim_foreground_dark_inverse"/>
    <item android:state_activated="true" android:color="@android:color/bright_foreground_dark_inverse"/>
    <item android:state_pressed="true" android:color="@android:color/dim_foreground_dark_inverse"/>
    <item android:state_enabled="false" android:color="@android:color/dim_foreground_dark_disabled"/>
    <item android:color="@android:color/dim_foreground_dark"/> <!-- not selected -->
</selector>




這份文檔在Android裡面稱做State List,一般用來變換一個繪圖元件在不同狀態下的顯示圖片。


狀態的定義有 (原文照拷比較原汁原味)


android:state_pressed
Boolean. "true" if this item should be used when the object is pressed (such as when a button is touched/clicked); "false" if this item should be used in the default, non-pressed state.
android:state_focused
Boolean. "true" if this item should be used when the object has input focus (such as when the user selects a text input); "false" if this item should be used in the default, non-focused state.
android:state_hovered
Boolean. "true" if this item should be used when the object is being hovered by a cursor; "false" if this item should be used in the default, non-hovered state. Often, this drawable may be the same drawable used for the "focused" state.
Introduced in API level 14.
android:state_selected
Boolean. "true" if this item should be used when the object is the current user selection when navigating with a directional control (such as when navigating through a list with a d-pad); "false" if this item should be used when the object is not selected.
The selected state is used when focus (android:state_focused) is not sufficient (such as when list view has focus and an item within it is selected with a d-pad).
android:state_checkable
Boolean. "true" if this item should be used when the object is checkable; "false" if this item should be used when the object is not checkable. (Only useful if the object can transition between a checkable and non-checkable widget.)
android:state_checked
Boolean. "true" if this item should be used when the object is checked; "false" if it should be used when the object is un-checked.
android:state_enabled
Boolean. "true" if this item should be used when the object is enabled (capable of receiving touch/click events); "false" if it should be used when the object is disabled.
android:state_activated
Boolean. "true" if this item should be used when the object is activated as the persistent selection (such as to "highlight" the previously selected list item in a persistent navigation view); "false" if it should be used when the object is not activated.
Introduced in API level 11.
android:state_window_focused
Boolean. "true" if this item should be used when the application window has focus (the application is in the foreground), "false" if this item should be used when the application window does not have focus (for example, if the notification shade is pulled down or a dialog appears).







回到secondary_text_dark.xml這份文件,每一個<item ... />表示一個狀態,所以在寫的時候需要確認到每一個<item ... />之間是互斥的關係。所以
<item android:state_pressed="true" android:state_enabled="false" android:color="@android:color/dim_foreground_dark_inverse_disabled"/>


表示當此TextView被按住(state_pressed="true")且此TextView是被除能的(disabled, 看上面的英文會比較理解這在說什麼)則此TextView的顏色變換成dim_foreground_dark_inverse_disabled所定義 (查一下它的顏色代碼是 #80323232)






我們常用State List來變換按鈕的狀態,譬如壓下按鈕然後放開會有一個顏色的變換讓使用者感覺到按鈕確實有被觸碰到。xml範例如下



<!-- filename: buttonbackground.xml  -->


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
   
    <item 
        android:drawable="@drawable/未按下時的圖片.png" 
        android:state_pressed="false" 
        android:state_selected="false"/>
    
    
    <item 
        android:drawable="@drawable/ 按下時的圖片.png 
        android:state_pressed="true"/>
</selector>


然後將Button的視圖的background屬性指向的這xml檔案即可。








1 則留言:

  1. Get a $50 free chip bonus at Slots of Vegas
    Slots of Vegas Online 출장안마 Casino Review https://septcasino.com/review/merit-casino/ - novcasino Welcome Bonus: Get $50 free chip bonus at Slots of Vegas Casino! Read on to learn aprcasino more about the latest  Rating: 3.5 · ‎Review by Casino Roll 바카라 사이트

    回覆刪除