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

Android 自訂美觀 Button 樣式的版面思路

編輯:關於 Android 程式設計

想讓 App 的按鈕更精緻,其實不必依賴圖片,光靠 XML 版面就能做出漂亮的漸層與狀態效果。以下是幾個參考來源與成果展示。

參考資料:

  • https://zrgiu.com/blog/2011/01/making-your-android-app-look-better/
  • https://www.dibbus.com/2011/02/gradient-buttons-for-android/
  • https://www.dibbus.com/2011/08/even-more-gradient-buttons-for-android/

看下面的效果,這些全都是純 XML 版面實作,一張圖片都沒用到:

版面檔示例

以下示範如何使用 selector 搭配 shape 與 gradient,做出按下時變色、平時帶漸層的按鈕:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid android:color="#449def" />
            <stroke android:width="1dp" android:color="#2f6699" />
            <corners android:radius="3dp" />
            <padding android:left="10dp" android:top="10dp"
                android:right="10dp" android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient android:startColor="#449def" android:endColor="#2f6699"
                android:angle="270" />
            <stroke android:width="1dp" android:color="#2f6699" />
            <corners android:radius="4dp" />
            <padding android:left="10dp" android:top="10dp"
                android:right="10dp" android:bottom="10dp" />
        </shape>
    </item>
</selector>

另一個帶漸層與圓角的 shape 寫法:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <solid android:color="#F000"/>
    <stroke android:width="1px" android:color="#BB000000" />
    <padding android:left="10dp" android:top="7dp"
        android:right="10dp" android:bottom="7dp" />
    <corners android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp"
        android:topLeftRadius="5dp" android:topRightRadius="5dp" />
    <gradient android:angle="90" android:startColor="#4747e0"
        android:centerColor="#5b5bcd" android:endColor="#6f6fcf"
        android:type="linear" />
</shape>

相關的開源範例專案可參考:

  • https://code.google.com/p/android-gradients-sample/downloads/list

另外推薦兩個隨手收藏的靈感來源:

  • https://www.webdesignshock.com/css-button
  • https://www.jb51.net/css/23418.html

透過 selector、shape、gradient 與 corners 的靈活組合,就能在不使用任何圖片的情況下,打造出具備漸層、圓角與按下回饋的精緻按鈕。

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