編輯:關於 Android 程式設計
在 Android 中,我們常透過 shape 來定義控件的外觀屬性。以下整理了 shape 的幾個常用標籤與屬性。
先看下面的程式碼:
<shape> <!-- 實心 --> <solid android:color="#ff9d77"/> <!-- 漸變 --> <gradient android:startColor="#ff8c00" android:endColor="#FFFFFF" android:angle="270" /> <!-- 描邊 --> <stroke android:width="2dp" android:color="#dcdcdc" /> <!-- 圓角 --> <corners android:radius="2dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape>
solid:實心,也就是填充的意思,android:color 用來指定填充的顏色。
gradient:漸變。android:startColor 和 android:endColor 分別為起始與結束顏色,android:angle 是漸變角度,必須為 45 的整數倍。此外,漸變預設模式為 android:type="linear"(線性漸變),也可指定為徑向漸變 android:type="radial",徑向漸變還需要指定半徑 android:gradientRadius="50"。
stroke:描邊
android:width="2dp" 為描邊寬度,android:color 為描邊顏色。我們還可以把描邊做成虛線,設定方式為 android:dashWidth="5dp" 與 android:dashGap="3dp",其中 dashWidth 表示一段橫線的寬度,dashGap 表示間隔的距離。
corners:圓角
android:radius 為角的弧度,值越大角就越圓。我們也可以把四個角設成不同角度:
<corners android:topRightRadius="20dp" 右上角 android:bottomLeftRadius="20dp" 左下角 android:topLeftRadius="1dp" 左上角 android:bottomRightRadius="0dp" 右下角 />
這裡有個地方要注意:bottomLeftRadius 對應的是右下角而非左下角,這一點有點反直覺,用時別搞錯就好。另外網上有人說設成 0dp 無效,但我在測試中發現是可以的,或許是版本已修正了這個問題;若真的無效,那就退而設成 1dp。
padding:間隔
這個就不用多說了,XML 佈局檔中經常用到。
大體上就是這樣,以下是一個具體的使用示例:把 shape 用在 Selector 中作為 Button 的背景,分別定義按鈕的一般狀態、獲得焦點狀態與按下時的狀態。
main.xml:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TestShapeButton" android:background="@drawable/button_selector" /> button_selector.xml: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="https://schemas.android.com/apk/res/android"> <item android:state_pressed="true" > <shape> <!-- 漸變 --> <gradient android:startColor="#ff8c00" android:endColor="#FFFFFF" android:type="radial" android:gradientRadius="50" /> <!-- 描邊 --> <stroke android:width="2dp" android:color="#dcdcdc" android:dashWidth="5dp" android:dashGap="3dp" /> <!-- 圓角 --> <corners android:radius="2dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> <item android:state_focused="true" > <shape> <gradient android:startColor="#ffc2b7" android:endColor="#ffc2b7" android:angle="270" /> <stroke android:width="2dp" android:color="#dcdcdc" /> <corners android:radius="2dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> <item> <shape> <solid android:color="#ff9d77"/> <stroke android:width="2dp" android:color="#fad3cf" /> <corners android:topRightRadius="5dp" android:bottomLeftRadius="5dp" android:topLeftRadius="0dp" android:bottomRightRadius="0dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> </selector>
運行效果如下圖:
一般狀態:

獲得焦點狀態:

按下狀態:

以上所述就是本文分享的 Android 中 shape 定義控件外觀的用法,希望大家喜歡。
Android Drawable 全景圖鑑:從 XML 節點到實現類
為什麼要系統地認識 Drawable 日常開發中,我們絕大多數時候並不直接 new 出某個 Drawable 實現類,而是寫 XML。只有當需要在運行時動態修改 Draw
Android Builder 模式詳解:從定義到實戰進化
Builder 模式出自《設計模式:可重複使用物件導向軟體的基礎》,在 Java 與 Android 開發中應用極廣。AlertDialog、OkHttp 的 Reque
Android 多媒體框架總結(十九):Camera2 框架 C/S 模型之 CameraService 啟動及與 Client 連接過程
Agenda: 一張圖看 Camera2 框架類圖、CameraService 啟動、ICameraService.cpp、Camera2Client.h 與 Camer
Android 使用 Visualizer 繪製動態音訊波紋
在音樂、錄音或語音互動類應用中,動態波紋是一種很常見的視覺呈現方式。它能直觀地表達聲音的強弱與節奏,讓使用者感受到聲音正在即時變化,也能提升整體操作的沉浸感。核心概念這類