編輯:關於 Android 開發
在 Android 開發中,很多時候我們並不需要一張切圖,只要一個 Shape 就能畫出按鈕底色、卡片邊框、圓角背景。以下把 <shape> 底下常用的標籤與屬性整理成一份可查的對照。
<solid android:color="#FF8C00" />
用 android:color 指定填滿的顏色,最簡單的一種背景。
<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
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
android:radius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp"
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp" />
android:radius 控制四個角的弧度,數值越大越圓。若要四角不同,就分別設定:topLeftRadius(左上)、topRightRadius(右上)、bottomLeftRadius(左下)、bottomRightRadius(右下)。
注意:這五個屬性同時設定時,
android:radius會失效,只有四個分角屬性生效。
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
可分別設定上下左右的內間距。
在 drawable 目錄下先定義兩個 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>
<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>
這裡特別在描邊中加了虛線參數,讓邊框變成虛線。
<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。
<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 好維護得多。
Paint API 系列(8.3.14):枚舉常量詳解與 ShadowLayer 陰影效果
Android 基礎入門教程——8.3.14 Paint 的枚舉常量與 ShadowLayer 陰影效果 本節引言 在 8.3.1 講 Paint 的方法參數時,我們
Android 常用 AlertDialog 對話框與自定義對話框實戰
Android 常用 AlertDialog 對話框與自定義對話框實戰 日常開發中常用的 Dialog 有確認框、單選框、多選框、列表框,以及自定義對話框。本篇透過一個完
Android GUI 系統之 SurfaceFlinger:VSync 訊號與合成流程
SurfaceFlinger 與 VSync VSync,即垂直同步,是圖形系統中相當重要的概念。它的目的在於讓畫面生成與螢幕刷新之間保持一致,避免畫面撕裂或延遲。 VS
Android 實作文字分享與接收:Intent.createChooser 的重要性
Android 實作文字分享與接收:Intent.createChooser 的重要性在 Android 中實作文字分享,通常會使用 ACTION_SEND,並指定 te