Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

玩轉 Android Drawable:用 XML 畫出圓角與邊框,並解掉 2.x 的圆角 BUG

編輯:關於 Android 程式設計

很久沒更新部落格,很多東西用完就忘,實在該記下來。除了自己複習,也推薦讀者養成寫筆記的習慣——開發 App 時真正用到的知識遠比一開始想像的多,甚至還會中途被派去用別的語言,不記錄就是學一個忘一個。

今天談兩件事:一是 drawable 在實務上的小技巧,二是 Android 2.x 上的一個圓角 BUG。

一、圓角按鈕不必找美工要圖

看到這樣的按鈕:

直覺做法是請美工切一張圓角背景圖,但多尺寸螢幕適配時還得再用 9-patch 處理一次,相當麻煩。其實只要是純色或簡單漸層,寫一份 drawable XML 就足夠了。

/drawable/btn_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="8dp" />
    <solid android:color="#ff0bd38a" />
</shape>

使用方式與一般圖片完全相同:

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/btn_bg"
    android:text="@string/app_name"
    android:textColor="@android:color/white"
    android:textSize="25sp" />

android:shape 決定基本形狀,除了 rectangle(矩形)還有 oval(橢圓)、line(線段)、ring(圓環),後三者實務上使用頻率不高。

二、corners:圓角

android:radius 為圓角半徑,數值越大越圓。四個角也可以各自指定:

<corners
    android:topLeftRadius="20dp"
    android:topRightRadius="20dp"
    android:bottomLeftRadius="0dp"
    android:bottomRightRadius="0dp" />

三、solid 與 stroke:填色與邊框

solid 是填色,android:color 指定顏色。stroke 則是描邊:

<stroke
    android:width="1dp"
    android:color="#ffaaaaaa" />

描邊還能做成虛線:android:dashWidth 是每一段實線的長度,android:dashGap 是段與段之間的間距。

需要邊框的場合寫成這樣即可:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="1dp"
        android:color="#ffaaaaaa" />
    <corners android:radius="8dp" />
    <solid android:color="@android:color/white" />
</shape>

四、gradient:漸層

android:startColor 與 android:endColor 分別是起始與結束色;android:angle 是漸層方向,且必須是 45 的整數倍。預設模式 android:type="linear" 為線性漸層,若要徑向漸層則設 android:type="radial" 並指定半徑 android:gradientRadius。

角度的意義可以這樣理解:把控制項內部想像成一塊色板,angle=0 由左至右、90 由上至下、180 由右至左,以此類推,數值就是色板繞中心的旋轉角度。

實務上漸層不太好調,多色變化又難拿捏,除非確定適用,否則直接請美工出圖反而更快。

五、padding

定義內容與邊界之間的距離,寫法單純,不多贅述。

六、進階:自訂進度條

用 layer-list 搭配 clip,就能做出圓角進度條:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />
            <solid android:color="#ffffffff" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <solid android:color="#ff00ff00" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <solid android:color="#ff00ff00" />
            </shape>
        </clip>
    </item>
</layer-list>

使用時指定 android:progressDrawable 即可:

<ProgressBar
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:progress="50"
    android:progressDrawable="@drawable/progress_horizontal" />

七、2.x 上的圓角 BUG:左下與右下對調

前述「四個角各自設定」的寫法,在 Android 2.x 上有個坑:bottomLeftRadius 與 bottomRightRadius 的實際效果是對調的。

照直覺寫:

<corners
    android:bottomLeftRadius="18dp"
    android:bottomRightRadius="0dp"
    android:topLeftRadius="18dp"
    android:topRightRadius="0dp" />

2.x 上的結果左右顛倒。若為了遷就 2.x 而交換寫法,到了 3.x 以上又會變成錯誤:

解法:用 drawable-v12 做版本分流

在 res/ 下新建 drawable-v12 目錄,把正確的寫法放進去;drawable/ 則保留遷就 2.x 的版本。因為 3.x 以上會優先從帶版本限定符的目錄取資源,兩邊就能各自拿到正確結果。

修改後的效果:

這個「用資源限定符做版本分流」的思路可以類推:以后再遇到类似在不同系統版本上行為不一致的問題,都能用同樣手法隔開,而不必在程式碼裡到處寫版本判斷。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved