1. 程式人生 > >Android Drawable資源中selector、layer-list和shape標籤詳解

Android Drawable資源中selector、layer-list和shape標籤詳解

在實際開發中,我們經常會對控制元件的樣式進行一些修改已滿足我們的要求,這時候就會引用 Drawable 資源的樣式檔案。

1、StateListDrawable 資源

        StateListDrawable 用於組織多個 Drawable 物件。當使用 StateListDrawable 作為目標元件的背景、前景圖片時,

StateListDrawable 物件所顯示的 Drawable 物件會隨目標元件狀態的改變而自動切換。

        定義 StateListDrawable 物件的 XML 檔案得根元素為 <selector.../> ,該元素可以包含多個 <item.../> 元素,該元素可指定如下屬

性:

        android:color 或 android:drawable :指定顏色或 Drawable 物件。

        android:state_xxx :制定一個特定狀態。

        例如如下語法格式:

<span style="font-size:14px;"><?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></span>

        其中 <item.../> 所支援的狀態有如下幾種:

        android:state_active :代表是否處於啟用狀態

        android:state_checkable :代表是否處於可勾選狀態

        android:state_checked :代表是否處於已勾選狀態

        android:state_enabled :代表是否處於可用狀態

        android:state_first :代表是否處於開始狀態

        android:state_focused :代表是否處於已得到焦點狀態

        android:state_last :代表是否處於結束狀態

        android:state_middle :代表是否處於中間狀態

        android:state_pressed :代表是否處於已被按下狀態

        android:state_selected :代表是否處於已被選中狀態

        android:state_window_focused :代表視窗是否處於已得到焦點狀態

2、LayerDrawable 資源

        與 StateListDrawable 有點類似,LayerDrawable 也可以包含一個 Drawable 陣列,因此係統將會按這些 Drawable 物件的陣列順

序來繪製它們,索引最大的 Drawable 物件將會被繪製在最上面。

        定義 LayerDrawable 物件的 XML 檔案的根元素為<layer-list.../> ,該元素可以包含多個 <item.../> 元素,該元素可指定如下屬

性:

        android:drawable :指定作為 LayerDrawable 元素之一的 Drawable 物件。

        android:id :為該 Drawable 物件指定一個標識。

        android:buttom | top | left | right :它們用於制定一個長度值,用於指定將該 Drawable 物件繪製到目標元件的指定位置。

        例如如下語法格式:

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 指定一個 Drawable 元素 -->
    <item android:id="@android:id/background"
        android:drawable="@drawable/xxx" />
</layer-list></span>

3、ShapeDrawable 資源

        ShapeDrawable 用於定義一個基本的幾何圖形(如矩形、圓形、線條等)。定義 ShapeDrawable 的 XML 檔案的根元素是 

<shape.../> 元素,該元素可指定如下屬性。

        android:shape=["rectangle" | "oval" | "line" | "ring"] :指定定義哪種型別的幾何圖形。

        定義 ShapeDrawable 物件的完整語法格式如下:

<?xml version="1.0" encoding="utf-8"?><!--android:shape=["rectangle" | "oval" | "line" | "ring"] > 矩形、橢圓、線形、環形-->
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 定義幾個圖形的四個角的弧度 -->
    <corners
        android:radius="integer(dp)"                    四個角的圓角弧度
        android:topLeftRadius="integer(dp)"             左上角的圓角弧度
        android:topRightRadius="integer(dp)"            右上角的圓角弧度
        android:bottomLeftRadius="integer(dp)"          左下角的圓角弧度
        android:bottomRightRadius="integer(dp)" />      右下角的圓角弧度
    <!-- 定義使用漸變色填充 -->
    <gradient
        android:angle="integer"                         漸變角度,必須是45的倍數,0從左到右,90從下到上,僅type="linear"時有效
        android:centerX="float"                         漸變中心相對X位置,有效範圍0-1
        android:centerY="float"                         漸變中心相對Y位置,有效範圍0-1
        android:centerColor="color"                     中間顏色
        android:endColor="color"                        結束顏色
        android:gradientRadius="integer"                漸變半徑,僅type="radial"時有效
        android:startColor="color"                      起始顏色
        android:type=["linear" | "radial" | "sweep"]    漸變樣式:線性(預設)、放射、掃描
        android:useLevel=["true" | "false"] />          使用LevelListDrawable時就要設定為true
    <!-- 定義幾何形狀的內邊距 -->
    <padding
        android:left="integer(dp)"                      左內邊距
        android:top="integer(dp)"                       上內邊距
        android:right="integer(dp)"                     右內邊距
        android:bottom="integer(dp)" />                 下內邊距
    <!-- 定義幾何形狀的大小 -->
    <size
        android:width="integer(dp)"
        android:height="integer(dp)" />
    <!-- 定義使用單種顏色填充 -->
    <solid
        android:color="color" />
    <!-- 定義為幾何形狀繪製邊框 -->
    <stroke
        android:width="integer(dp)"                     描邊的寬度
        android:color="color"                           描邊的顏色
        android:dashWidth="integer(dp)"                 虛線的寬度,0表示實線
        android:dashGap="integer(dp)" />                虛線的間隔寬度
</shape>