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

Android 按鈕同時設定背景與圓角

編輯:關於 Android 程式設計

在 Android 中,若按鈕需要背景圖片,又希望保留圓角與邊框,直接切一張圓角圖是最簡單的方式;但若想保持背景圖可替換,也可以用 shape 與 layer-list 組合。

建立圓角 shape

先建立一個 shape 資源,設定 corners 與 stroke:

<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <corners android:radius="10dp"/>
    <stroke android:width="1dp" android:color="#FF6238" />
</shape>

建立 layer-list

再用 layer-list 把背景圖與 shape 疊起來:

<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/ic_launcher"/>
    <item android:drawable="@drawable/shape"/>
</layer-list>

套用至按鈕

最後在版面配置或程式碼中把 background 指向這個 layer-list:

<TextView
    android:gravity="center"
    android:background="@drawable/list"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:textSize="16sp"
    android:textColor="#FF0000"
    android:text="按鈕" />

這種做法能讓背景圖與圓角樣式分離,後續修改 Radius 或顏色時也比較容易。

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