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

Android 自訂 Button 樣式:讓按鈕點擊時變色

編輯:關於 Android 程式設計

預設的 Button 外觀通常撐不起設計稿的要求。要做出「按下去顏色會變」的效果,最正統的做法是用 selector 搭配 shape,完全不需要寫任何 Java 程式碼。

效果如下:

一、selector + shape 樣式

在 drawable 目錄新增一個 Drawable resource file(new → Drawable resource file),把不同狀態的樣式寫進 selector 的 item 裡:

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#73c4f3" />
            <stroke android:width="2dp" android:color="#ffffff" />
            <corners android:radius="20dp" />
            <padding android:bottom="5dp" android:left="10dp" android:right="10dp" android:top="5dp" />
            <gradient android:angle="270" android:endColor="#8accf2" android:startColor="#8accf2" android:type="sweep" />
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="#3fb3f6" />
            <stroke android:width="2dp" android:color="#ffffff" />
            <corners android:radius="20dp" />
            <padding android:bottom="5dp" android:left="10dp" android:right="10dp" android:top="5dp" />
        </shape>
    </item>
</selector>

第一個 item 對應「按下」狀態,第二個沒寫任何 state 條件的,就是預設狀態。

二、把樣式抽成 style

如果同一個畫面有很多按鈕共用外觀,把它抽成 style 會好維護得多:

 <style name="rectangle">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">15sp</item>
    <item name="android:textColor">#ffffff</item>
    <item name="android:background">@drawable/buttonclickstyle</item>
</style>

三、在配置檔中套用

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.ly.blogtest.MainActivity">

    <Button
        android:id="@+id/button"
        style="@style/rectangle"
        android:text="@string/btn1" />
</RelativeLayout>

item 可用的狀態屬性

selector 真正的威力來自這些狀態標記,把它們記熟,幾乎所有互動回饋都能純宣告式完成:

android:state_pressed 是否被按下,例如手指觸摸或點擊按鈕。

android:state_focused 是否取得焦點,例如使用者用方向鍵選到某個輸入框。

android:state_hovered 游標是否懸停其上,通常與 focused 狀態同時發生,為 API 4.0 新增的特性。

android:state_selected 是否被選取。它與 focus 並不完全等價——ListView 被選取時,其中的子元件可能各自處於不同的 focus 狀態。

android:state_checkable 元件是否可勾選,例如 RadioButton 就是可勾選的。

android:state_checked 是否已被勾選。

android:state_enabled 是否可接收觸摸或點擊事件。

android:state_activated 是否處於啟用狀態。

android:state_window_focused 應用程式是否在前台。當通知欄被拉下、或有對話框彈出時,它就不再是前台狀態。

兩個容易踩到的細節

當 selector 裡有多個 item 時,系統會由上而下逐一比對,取第一個符合的,而不是挑「最匹配」的那個。因此範圍較大的預設項目要放在最後。

其次,若某個 item 完全沒有標註任何狀態,它就會匹配任何狀態——這正是上面範例中預設樣式能生效的原因。

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