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

Android 基礎控件 Button 詳解:用 StateListDrawable 打造多狀態按鈕

編輯:關於 Android 開發

在日常的 Android 開發中,Button 是最常被使用的交互控件之一。想要讓按鈕在不同狀態下呈現不同的外觀,離不開 StateListDrawable 的協助。在正式動手寫代碼之前,我們先來梳理一下 StateListDrawable 的相關知識。

一、什麼是 StateListDrawable

StateListDrawable 是 Drawable 資源的一種類型,其核心標籤是 selector。當我們把 Button 的 background 屬性設置為 @drawable/selector_name 之後,系統就會根據按鈕當前的狀態自動切換顯示的樣式。這種機制不僅適用於 Button,其他控件同樣可以使用,只是它最常被應用在按鈕上。

下面列舉 StateListDrawable 中常用的幾個狀態屬性:

  • drawable:引用的 Drawable 位圖,通常放在最前面作為控件的默認狀態。
  • state_focused:控件是否獲得焦點。
  • state_window_focused:控件是否獲得窗口焦點。
  • state_enabled:控件是否可用。
  • state_checkable:控件是否可被勾選,例如 checkbox。
  • state_checked:控件是否已被勾選。
  • state_selected:控件是否被選中,常用於帶滾輪的場景。
  • state_pressed:控件是否被按下。
  • state_active:控件是否處於活動狀態,例如 slidingTab。
  • state_single:控件包含多個子控件時,確定是否只顯示一個子控件。
  • state_first:控件包含多個子控件時,確定第一個子控件是否處於顯示狀態。
  • state_middle:控件包含多個子控件時,確定中間子控件是否處於顯示狀態。
  • state_last:控件包含多個子控件時,確定最後一個子控件是否處於顯示狀態。

二、實例:實現按鈕按下效果與圓角

接下來我們通過一個完整的例子,實現按鈕在不同狀態下的樣式切換以及圓角效果。

首先是 Java 代碼:

package com.example.test3;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
    private Button btn1;
    private Button btn2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.btn_one);
        btn2 = (Button) findViewById(R.id.btn_two);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (btn2.getText().toString().endsWith("按鈕不可用")){
                    btn1.setEnabled(false);
                    btn2.setText("按鈕可用");
                }else{
                    btn1.setEnabled(true);
                    btn2.setText("按鈕不可用");
                }
            }
        });
    }
}

接著是 StateListDrawable 的定義文件,其中 shape 的用法此前已經介紹過:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <!--按下按鈕-->
    <item android:state_pressed="true">
        <shape>
            <stroke android:width="5px" android:color="@android:color/holo_red_light"/>
            <corners android:radius="15dp"/>
            <solid android:color="@android:color/holo_red_light"/>
        </shape>
    </item>
    <!-- 按鈕不可用-->
    <item android:state_enabled="false">
        <shape>
            <stroke android:width="5px" android:color="@android:color/darker_gray"/>
            <corners android:radius="15dp"/>
            <solid android:color="@android:color/darker_gray"/>
        </shape>
    </item>
    <!--其他狀況-->
    <item>
        <shape>
            <stroke android:width="5px" android:color="@android:color/holo_blue_bright"/>
            <corners android:radius="15dp"/>
            <solid android:color="@android:color/holo_blue_bright"/>
        </shape>
    </item>
</selector>

最後是布局文件:

<?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"
    android:background="#ffffff">
    <Button
        android:id="@+id/btn_one"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@drawable/btn_bg1"
        android:text="按鈕"
        android:textSize="24sp"/>
    <Button
        android:id="@+id/btn_two"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_marginTop="10dp"
        android:text="按鈕不可用"
        android:textSize="24sp"/>
</LinearLayout>

通過以上配置,按鈕就能在按下、禁用和默認狀態之間流暢切換外觀,同時呈現漂亮的圓角邊框。

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