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

Android 自定義 Button 按鈕:用 XML 打造多狀態樣式

編輯:關於 Android 程式設計

在 Android 中自定義 Button 按鈕,主要可以分成兩種形式:

  1. 透過自定義 MyButton 類別來繼承 Button,把所有效果在類別中實作。
  2. 透過 XML 檔案來改變 Button 的樣式與顏色。

本文先介紹第二種做法,也就是純粹用 XML 來定義按鈕外觀,之後再補上封裝自定義 Button 類別的部分。

版面配置:testc.xml

先在版面檔中放兩個按鈕,一個套用自定義樣式,一個保留系統原始樣式作為對照:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="測試按鈕"
        android:background="@drawable/button_style"
        />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="原始按鈕"
        />

</LinearLayout>

對應的 Activity 只需載入這個版面即可:

package com.example.blueapp;

import android.app.Activity;
import android.os.Bundle;

public class TestcActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testc);
    }
}

按鈕樣式:button_style.xml

關鍵在於使用 selector 標籤,為按鈕的不同狀態定義各自的 shape:

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

    <item android:state_focused="true">
        <shape>
            <gradient android:startColor="#ffc2b7" android:endColor="#ffc2b7"
                android:angle="270" />
            <stroke android:width="1dip" android:color="#f403c9" />
            <corners android:radius="2dp" />
            <padding android:left="10dp" android:top="10dp"
                android:right="10dp" android:bottom="10dp" />
        </shape>
    </item>

    <item>
        <shape>
            <gradient android:startColor="#000000" android:endColor="#ffffff"
                android:angle="180" />
            <stroke android:width="1dip" android:color="#f403c9" />
            <corners android:radius="5dip" />
            <padding android:left="10dp" android:top="10dp"
                android:right="10dp" android:bottom="10dp" />
        </shape>
    </item>
</selector>

各屬性含義說明

  • gradient(漸變):startColor 為起始顏色,endColor 為結束顏色,angle 為漸變角度(只能是 90 的倍數,0 表示由左到右,90 表示由下到上,以此類推逆時針方向)。
  • stroke(邊框):width 為邊框寬度,color 為邊框顏色。
  • corners(圓角):radius 為圓角半徑,設為 0 即為直角。
  • padding(內距):決定文字內容的相對位置。

透過 selector 為 pressed、focused 與預設三種狀態分別設定不同外觀,就能輕鬆做出具有視覺回饋效果的漂亮按鈕。

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