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

Android 界面美化:Shape Drawable 用法完全解析

編輯:關於 Android 程式設計

Shape 是在 XML 中定義的一類 Drawable,用來快速做出圓角、漸層、邊框、實心填色等美觀的界面元素,非常適合 Android 的 UI 美化。

基本結構

檔案位置:res/drawable/filename.xml,此檔名即為資源 ID。編譯後的資源型別為 GradientDrawable 的資源參照。在 Java 中用 R.drawable.filename 引用,在 XML 中用 @[package:]drawable/filename 引用。

語法範式如下:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] >
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="integer"
        android:centerY="integer"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>

shape 的 android:shape 屬性

  • rectangle:填滿整個 View 的長方形,是預設值。
  • oval:符合 View 尺寸的橢圓形。
  • line:橫跨 View 寬度的水平線,需搭配 <stroke> 定義線寬。
  • ring:環形。僅在 shape="ring" 時使用的屬性有 android:innerRadius(內圈半徑)、android:innerRadiusRatio(內半徑佔環寬的比例,預設 9)、android:thickness(環的厚度)、android:thicknessRatio(厚度佔環寬的比例,預設 3)。

<corners> 圓角

為形狀建立圓角,僅適用於長方形。可用 android:radius 設定四角統一圓角,或用 topLeftRadius、topRightRadius、bottomLeftRadius、bottomRightRadius 分別設定。注意:每個角初始都必須給大於 1 的半徑,否則不會有圓角;若某些角不想圓滑,可先用 android:radius 設一個大於 1 的預設值,再逐一覆寫為 0dp。另外要特別小心——bottomLeftRadius 指的是「右下角」,bottomRightRadius 才是「左下角」,別記反了。

<gradient> 漸層

指定形狀的漸層填色。startColor 與 endColor 為起始與結束顏色;android:angle 為漸層角度,必須是 45 的整數倍。預設模式 android:type="linear"(線性漸層),也可設為 "radial"(徑向漸層,需指定 android:gradientRadius)或 "sweep"。關於 angle 的實際行為,經實驗:angle=0 由左到右、90 由上到下、180 由右到左、360 與 0 相同——可理解為把控制項內部當成繞中心旋轉的色板,angle 即為旋轉角度(須為 45 的倍數)。

<solid> 實心填色

用 android:color 指定填滿形狀的單一顏色。

<stroke> 描邊

用 android:width 設定描邊寬度、android:color 設定顏色。若要虛線,可加上 android:dashWidth(每段實線的寬度)與 android:dashGap(段與段之間的間隔)。

<padding> 內距

定義內容離邊界的距離,效果等同 android:padding_left、android:padding_right 等屬性,填的是 View 內容的位置而非形狀本身。

完整範例

XML 檔存於 res/drawable/gradient_box.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45"/>
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <corners android:radius="8dp" />
</shape>

在布局中套用為 View 背景:

<TextView
    android:background="@drawable/gradient_box"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" />

或用程式碼套用:

Resources res = getResources();
Drawable shape = res.getDrawable(R.drawable.gradient_box);
TextView tv = (TextView) findViewByID(R.id.textview);
tv.setBackground(shape);

掌握以上元素,就能用純 XML 輕鬆做出圓角、漸層、邊框等各式美觀的控制項背景。

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