StateListDrawable 資源

StateListDrawable 用於組織多個 Drawable 對象。當使用 StateListDrawable 做爲目標組件 的背景、前景圖片時,StateListDrawable 對象所顯示的  Drawable 對象會隨目標組件狀態的改 變而自動切換。
定義 StateListDrawable 對象的 XML 文件的根元素爲<selector.../>,該元素能夠包含多個<item.../>元素,該元素可指定以下屬性。 
  • android:color 或 android:drawable:指定顏色或 Drawable 對象。 
  • android:state_xxx:指定一個特定狀態。 
例如以下語法格式:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 指定特定狀態下的顏色 -->
<item android:color="hex_color""
      android:state_pressed=["true" | "false"] />
</selector>
 
 
高亮顯示正在輸入的文本示例
 
my_image.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 指定得到焦點時的顏色 -->
    <item android:state_focused="true"
        android:color="#f44"/>
    <!-- 指定失去焦點時的顏色 -->
    <item android:state_focused="false"
        android:color="#eee"/>
</selector>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<!-- 使用StateListDrawable資源 -->
<EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="@drawable/my_image"
    />
<EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="@drawable/my_image"
    />
</LinearLayout>
 
相關文章
相關標籤/搜索