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

詳解 Android Shape 標籤:打造自訂控制項外觀

編輯:關於 Android 開發

Android 開發者在實作過程中,經常得兼顧一些美工相關的工作,而這恰恰是不少工程師比較不拿手的部分。本文就從簡潔易用的角度,和大家聊聊 shape 的用法。

在 Android 中,我們常用 shape 來定義控制項的一些顯示屬性。在讀過一些 shape 的用法技巧後,我對它有了大致的理解,下面做個整理。

先看下面的程式碼:

  1. <shape>
  2.     <!-- 實心 -->
  3.     <solid android:color="#ff9d77"/>
  4.     <!-- 漸變 -->
  5.     <gradient
  6.          android:startColor="#ff8c00"
  7.          android:endColor="#FFFFFF"
  8.          android:angle="270" />
  9.     <!-- 描邊 -->
  10.     <stroke
  11.          android:width="2dp"
  12.          android:color="#dcdcdc" />
  13.     <!-- 圓角 -->
  14.     <corners
  15.          android:radius="2dp" />
  16.     <padding
  17.          android:left="10dp"
  18.          android:top="10dp"
  19.          android:right="10dp"
  20.          android:bottom="10dp" />
  21. </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"

其中 android:dashWidth 表示一段「-」橫線的寬度,android:dashGap 則表示線段之間隔開的距離。

corners:圓角

android:radius 為圓角的弧度,值越大,角就越渾圓。

我們也能把四個角設成不同弧度,方法如下:

  1. <corners
  2.         android:topRightRadius="20dp"    右上角
  3.         android:bottomLeftRadius="20dp"    右下角
  4.         android:topLeftRadius="1dp"    左上角
  5.         android:bottomRightRadius="0dp"    左下角
  6. />

這裡有個地方要特別留意:bottomLeftRadius 其實對應的是「右下角」,而不是左下角,這點有點反直覺,但只要記住別搞錯就好。

另外網路上有人說設成 0dp 無效,不過我在實測中發現是可以的,我用的是 2.2,也許這個問題已被修正;若真的無效,那就只能設成 1dp 了。

padding:間隔

這個就不多說了,XML 版面配置檔中經常會用到。

大體上就是這樣。以下是一個具體的使用範例:把它用在 Selector 中作為 Button 的背景,分別定義按鈕的一般狀態、取得焦點狀態,以及按下時的狀態,程式碼如下:

main.xml:

  1. <Button
  2.      android:layout_width="wrap_content"
  3.      android:layout_height="wrap_content"
  4.      android:text="TestShapeButton"
  5.      android:background="@drawable/button_selector"
  6. />

button_selector.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector
  3.      xmlns:android="https://schemas.android.com/apk/res/android">
  4.     <item android:state_pressed="true">
  5.         <shape>
  6.             <!-- 漸變 -->
  7.             <gradient
  8.                  android:startColor="#ff8c00"
  9.                  android:endColor="#FFFFFF"
  10.                  android:type="radial"
  11.                  android:gradientRadius="50" />
  12.             <!-- 描邊 -->
  13.             <stroke
  14.                  android:width="2dp"
  15.                  android:color="#dcdcdc"
  16.                  android:dashWidth="5dp"
  17.                  android:dashGap="3dp" />
  18.             <!-- 圓角 -->
  19.             <corners
  20.                  android:radius="2dp" />
  21.             <padding
  22.                  android:left="10dp"
  23.                  android:top="10dp"
  24.                  android:right="10dp"
  25.                  android:bottom="10dp" />
  26.         </shape>
  27.     </item>
  28.     <item android:state_focused="true">
  29.         <shape>
  30.             <gradient
  31.                  android:startColor="#ffc2b7"
  32.                  android:endColor="#ffc2b7"
  33.                  android:angle="270" />
  34.             <stroke
  35.                  android:width="2dp"
  36.                  android:color="#dcdcdc" />
  37.             <corners
  38.                  android:radius="2dp" />
  39.             <padding
  40.                  android:left="10dp"
  41.                  android:top="10dp"
  42.                  android:right="10dp"
  43.                  android:bottom="10dp" />
  44.         </shape>
  45.     </item>
  46.     <item>
  47.         <shape>
  48.             <solid android:color="#ff9d77"/>
  49.             <stroke
  50.                  android:width="2dp"
  51.                  android:color="#fad3cf" />
  52.             <corners
  53.                  android:topRightRadius="5dp"
  54.                  android:bottomLeftRadius="5dp"
  55.                  android:topLeftRadius="0dp"
  56.                  android:bottomRightRadius="0dp"
  57.             />
  58.             <padding
  59.                  android:left="10dp"
  60.                  android:top="10dp"
  61.                  android:right="10dp"
  62.                  android:bottom="10dp" />
  63.         </shape>
  64.     </item>
  65. </selector>

運行效果如下:

一般狀態:

取得焦點狀態:

按下狀態:

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