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

Android UI 透明按鈕與 ImageButton 圖層用法整理

編輯:關於 Android 程式設計

Android UI 透明按鈕與 ImageButton 整理

在 Android 介面中,透明按鈕常使用 shape 資源建立半透明背景,再搭配 Button 或 ImageButton 呈現。透過 shape、corners、padding 與 color,可快速做出圓角、半透明且可重複使用的按鈕樣式。

shape 背景

<shape android:shape="rectangle"
    xmlns:android="https://schemas.android.com/apk/res/android">
    <solid android:color="#55222222"/>
    <corners android:radius="5.0dip" />
    <padding android:left="6.0dip"
        android:top="6.0dip"
        android:right="6.0dip"
        android:bottom="6.0dip"/>
</shape>

Button 套用背景

<Button
    android:id="@+id/button1"
    android:layout_width="100px"
    android:layout_height="100px"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/shape"
    android:gravity="center"
    android:text="360衛士"
    android:textColor="@android:color/white" />

ImageButton 的 src 與 background

ImageButton 中,android:background 是底圖,android:src 是前景圖。src 會疊在 background 上方,因此若需要圖層效果,可將背景圖放在 background,前景圖放在 src。通常 background 尺寸可大於 src,以呈現底框或裝飾效果。

<ImageButton
    android:id="@+id/play_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="@drawable/shape"
    android:src="@drawable/buttons_bg1" />
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved