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

Android 自定義控制元件(一):用 shape 畫出圓角按鈕

編輯:關於 Android 開發

Android 自定義控制元件(一):用 shape 畫出圓角按鈕

原生 Button 的外觀實在樸素,專案裡幾乎一定會碰到「圓角、換底色、加描邊」這類需求。與其請設計師切一張九宮格圖,不如直接用 shape 這張可以描述出來的背景:改幾個屬性就能換色、換圓角半徑、換邊框樣式,體積也比圖片小得多。

先看成品:

一、定義 shape 背景

在 drawable 目錄上按右鍵 → New → Drawable resource file,檔名取 buttonstyle。這裡有個容易踩到的小細節:Android Studio 產生的預設根節點是 selector,要手動改成 shape,否則後面的 solid、corners 都不會生效。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#4ec5ff" />

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

    <corners android:radius="10dp" />

    <stroke
        android:width="2dp"
        android:color="#ffffff"
        android:dashGap="270dp" />

</shape>

這段描述的其實很直白:底色填 #4ec5ff,四個角各切 10dp 圓角,再畫一圈 2dp 的白色描邊,內距統一 12dp。

二、把樣式抽成 style

如果同一個 App 裡有多顆長相類似的按鈕,把重複屬性收進 styles.xml 會省下不少力氣,之後只要引用樣式就好。

<style name="rectangle">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">15sp</item>
    <item name="android:textColor">#ffffff</item>
    <item name="android:background">@drawable/buttonstyle</item>
</style>

三、在版面配置中引用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.ly.blogtest.MainActivity">

    <Button
        android:id="@+id/button"
        style="@style/rectangle"
        android:text="@string/btn1" />

</RelativeLayout>

一行 style="@style/rectangle" 就把背景、字色、內距全部帶進來了,後續要換主題色只需改 shape 裡的 solid。

shape 的四種基本形狀

android:shape 決定這張背景長什麼樣子,有四個可選值:

  • rectangle:矩形,也是預設值。直角矩形、圓角矩形、甚至超大圓角形成的弧形邊都靠它。
  • oval:橢圓形,最常見的用法是畫正圓(寬高相等時)。
  • line:線形,可以畫實線也可以畫虛線,常用來做分隔線。
  • ring:環形,搭配漸層可以做環形進度條。

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:填色,例如 #ff9d77。

padding:內容與邊界的內距

  • android:left / android:right / android:top / android:bottom:分別控制四個方向的內間距。

gradient:漸層

漸層可分為線性、放射與掃描三種,是做質感按鈕很好用的一招。

  • android:type:漸層類型
    • linear:線性漸層,預設值。
    • radial:放射漸層,使用時必須一併設定 android:gradientRadius。
    • sweep:掃描式漸層,常見於環形進度條。
  • android:startColor:起始色。
  • android:endColor:結束色。
  • android:centerColor:中間色。
  • android:angle:漸層角度,僅線性漸層有效,且必須是 45 的倍數。0 表示由左至右,90 表示由下往上。
  • android:centerX:漸層中心的相對 X 座標,僅放射漸層有效,範圍 0.0~1.0,預設 0.5(正中央)。
  • android:centerY:漸層中心的相對 Y 座標,規則同上。
  • android:gradientRadius:漸層半徑,僅 radial 類型使用。
  • android:useLevel:設為 true 時可在 LevelListDrawable 中使用。

corners:圓角

只對 rectangle 有效。四個角可以各自指定半徑,也可以只用 radius 統一設定;若把半徑開到 200dp 這類誇張數值,就會變成弧形邊。

  • android:radius:統一圓角半徑,會被下面幾個特定屬性覆寫。
  • android:topLeftRadius:左上角半徑。
  • android:topRightRadius:右上角半徑。
  • android:bottomLeftRadius:左下角半徑。
  • android:bottomRightRadius:右下角半徑。

stroke:描邊

  • android:color:邊框顏色。
  • android:width:邊框寬度。
  • android:dashWidth:虛線時每一段實線的長度。
  • android:dashGap:虛線時兩段實線之間的間距。

把這幾個標籤組合起來,幾乎能應付日常九成的按鈕視覺需求,而且完全不用切圖。

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