萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> Android 開發

Android Shape 與 Selector 結合實戰:漸變、描邊、圓角、狀態切換一站式指南

Shape 定義幾何圖形,Selector 根據狀態切換 Drawable,兩者結合可實現豐富的按鈕、卡片、輸入框背景效果,且無需圖片資源,包體積極小、縮放無失真。本文系統講解 Shape 五大子標簽、Selector 狀態屬性、實戰按鈕背景與圖片遮罩案例。

一、Shape 五大核心子標簽

標簽核心屬性作用
<solid>android:color填充顏色
<gradient>startColor, endColor, angle (45倍數), type (linear/radial/sweep), gradientRadius (radial必填)漸變填充
<stroke>width, color, dashWidth, dashGap描邊,支援虛線
<corners>radius, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius圓角,可單獨控制四角
<padding>left/top/right/bottom內容邊距,影響文字/圖標位置

二、實戰案例 1:按鈕背景 (按下/正常/不可用三態)

res/drawable/btn_bg_normal.xml (Shape)

<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <gradient
        android:type="radial"
        android:gradientRadius="50"
        android:startColor="#ff8c00"
        android:endColor="#FFFFFF" />
    <stroke android:width="2dp" android:color="#dcdcdc"
        android:dashWidth="5dp" android:dashGap="3dp" />
    <corners android:radius="5dp" />
    <padding android:left="10dp" android:top="10dp"
        android:right="10dp" android:bottom="10dp" />
</shape>

res/drawable/btn_bg_pressed.xml (Shape)

<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <solid android:color="#ff6600" />
    <stroke android:width="2dp" android:color="#cc5500" />
    <corners android:radius="5dp" />
    <padding android:left="10dp" android:top="10dp"
        android:right="10dp" android:bottom="10dp" />
</shape>

res/drawable/btn_bg.xml (Selector)

<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/btn_bg_pressed" />
    <item android:state_enabled="false" android:drawable="@drawable/btn_bg_disabled" />
    <item android:drawable="@drawable/btn_bg_normal" /> <!-- 默認狀態放最後 -->
</selector>

佈局引用

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="提交"
    android:background="@drawable/btn_bg" />

三、實戰案例 2:圖片圓角遮罩 (ImageView 前景)

res/drawable/shape_image_mask.xml

<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <solid android:color="#ff9d77" />
    <stroke android:width="2dp" android:color="#fad3cf" />
    <corners
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp"
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp" />
    <padding android:left="10dp" android:top="10dp"
        android:right="10dp" android:bottom="10dp" />
</shape>

使用方式:imageView.setForeground(ContextCompat.getDrawable(context, R.drawable.shape_image_mask)) 或 XML android:foreground。

四、Selector 狀態屬性完整列表

  • state_pressed、state_focused、state_selected、state_checked、state_enabled、state_window_focused、state_activated (API 11+)、state_hovered (API 14+)
  • 組合邏輯:從上到下匹配第一個滿足條件的 item,默認項目 (無 state 屬性) 必須放最後。

五、進階技巧

  • Layer-list 疊加: 在 Selector item 中引用 <layer-list> 實現圖標+背景疊加。
  • Ripple 效果 (API 21+): 使用 <ripple> 替代 Selector 實現 Material Design 水波紋。
  • VectorDrawable 替代 Shape: 複雜路徑、動畫轉換優先用 Vector。

掌握 Shape + Selector 組合,能以極低成本實現 90% 以上的 UI 背景狀態效果,是 Android UI 開發必備基本功。

copyright © 萬盛學電腦網 all rights reserved