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

Android Shape 標籤完整整理:填色、漸層、描邊與圓角

編輯:關於 Android 開發

Android Shape 標籤完整整理:填色、漸層、描邊與圓角

在 Android 開發中,很多時候我們並不需要一張切圖,只要一個 Shape 就能畫出按鈕底色、卡片邊框、圓角背景。以下把 <shape> 底下常用的標籤與屬性整理成一份可查的對照。

solid:純色填滿

<solid android:color="#FF8C00" />

用 android:color 指定填滿的顏色,最簡單的一種背景。

gradient:漸層

<gradient
    android:startColor="#FF8C00"
    android:endColor="#FFFFFF"
    android:angle="45"
    android:type="linear" />
  • android:startColor 與 android:endColor 分別是起始色與結束色。
  • android:angle 是漸層角度,必須是 45 的整數倍。
  • android:type 預設為 linear(線性漸層);若要改成徑向漸層則設為 radial,此時必須額外指定半徑 android:gradientRadius="50"。

角度對應的方向如下圖:

stroke:描邊

<stroke
    android:width="2dp"
    android:color="#DCDCDC" />

android:width 是邊線寬度,android:color 是邊線顏色。想要虛線效果,再加上這兩個屬性:

<stroke
    android:width="2dp"
    android:color="#DCDCDC"
    android:dashWidth="5dp"
    android:dashGap="3dp" />

其中 android:dashWidth 是每一段實線的寬度,android:dashGap 是段與段之間的間隔。

corners:圓角

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

android:radius 控制四個角的弧度,數值越大越圓。若要四角不同,就分別設定:topLeftRadius(左上)、topRightRadius(右上)、bottomLeftRadius(左下)、bottomRightRadius(右下)。

注意:這五個屬性同時設定時,android:radius 會失效,只有四個分角屬性生效。

padding:內距

<padding
    android:left="10dp"
    android:top="10dp"
    android:right="10dp"
    android:bottom="10dp" />

可分別設定上下左右的內間距。


動手做一個 ShapeDemo

在 drawable 目錄下先定義兩個 XML。

button_bg.xml(常態)

<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <stroke
        android:width="2dp"
        android:color="#dcdcdc" />
    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
</shape>

button_pressed_bg.xml(按下)

<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="#ff8c00"
        android:endColor="#FFFFFF"
        android:gradientRadius="50"
        android:type="radial" />
    <stroke
        android:width="2dp"
        android:color="#dcdcdc"
        android:dashGap="3dp"
        android:dashWidth="5dp" />
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
</shape>

這裡特別在描邊中加了虛線參數,讓邊框變成虛線。

button.xml(狀態選擇器)

<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/button_pressed_bg" />
    <item android:drawable="@drawable/button_bg" />
</selector>

意思是:正常狀態顯示 button_bg,被按下時顯示 button_pressed_bg。

activity_main.xml

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/button"
    android:text="TestShapeButton" />

把 background 指向 drawable 下的 button.xml 即可。

運行結果如下:

小結

Shape 的價值在於用宣告式的方式取代切圖:純色用 solid、漸層用 gradient、邊框用 stroke、圓角用 corners,再搭配 selector 就能做出完整的狀態回饋。檔案小、任意縮放不失真,也能隨主題統一調整配色——比一堆 PNG 好維護得多。

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