標籤:android http io color ar 使用 sp for 檔案
在使用者進行操作的時候總是會提示出不同的狀態,比如當我們按下button時,游標移動到button上並沒有按下以及目前狀態不在該button時總會顯示不同的狀態,在Android系統中提供給我們一種方便與實現這種功能的方法即:state list drawable。
StateListDrawable是在XML中定義的drawable對象,我們可以通過設定不同item下的圖片來顯示不同狀態,它的XML檔案定義如下:
<?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_selected=["true" "false"]
android:state_active=["true" "false"]
android:state_checkable=["true" "false"]
android:state_checked=["true" "false"]
android:state_enabled=["true" "false"]
android:state_window_focused=["true" "false"] />
</selector>
<selector>為根節點,其下子節點只有一個為item
android:constantSize: boolean型,預設為false,
android:dither:boolean型,預設為true,當位元影像與螢幕的像素配置不一樣時(例如,一個ARGB為8888的位元影像與RGB為555的螢幕)會自行遞色(dither)。設定為false時不可遞色。
android:variablePadding:boolean型,預設為false,當設定為true時,則drawable的padding值隨當前選擇的狀態而改變。
<item>通過其屬性定義當選中某種狀態的時候應該顯示的圖片資源;
android:drawable:必須的參數,drawable資源;
android:state_pressed:boolean型,設定為true時表示當對象被按下時該item會顯示或者說生效,為false時表示該item為預設狀態非選中狀態;
android:state_focused:boolean型,為true時表示該item生效為焦點在對象上時,false為非選中狀態;
android:state_selected:boolean型,同上功能,該屬性工作表示的時被選擇狀態;
android:state_checkable:boolean型,僅僅用在可以選擇widget上,為true表示可選擇,為false表示不可選;
android:state_checked:boolean型,為true時,表示當選中時該item生效,false為未選中時生效;
android:state_enabled:boolean型,當為true時,該item在對象可啟用時生效,如該對象可以接受觸摸或者點擊事件時;
android:state_window_focused:boolean型,為true時,表示該item在當前視窗焦點為該應用程式視窗時生效也就是說該應用程式視窗為foreground,否則為false;
當我們要定義一個自己需要的狀態選擇功能的檔案時,其實現方式主要可簡括為兩步:
1. 在/res/drawable 目錄下建立自己需要的.xml檔案如select_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:drawable="@drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/button_focused" /> <!-- focused -->
<item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>
2.在layout xml檔案中引用:如
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/select_button" />
PS完整執行個體:
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 沒有焦點時的背景顏色 -->
<item android:state_window_focused="false"
android:drawable="@color/transparent" />
<!-- 非觸摸模式下獲得焦點並單擊時的背景顏色 -->
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@color/bgred" />
<!--觸摸模式下單擊時的背景顏色 -->
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@color/bgred" />
<!--選中時的背景顏色 -->
<item android:state_selected="true" android:drawable="@color/bgred" />
<!--獲得焦點時的背景 顏色-->
<item android:state_focused="true" android:drawable="@color/bgred" />
</selector>
Android中StateListDrawable和Selector的使用