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

Android 設置頁開關效果實現:style 復用與 selector 自定義

編輯:Android 開發教學

最終效果如下:

具體來說實現了兩件事:CheckBox 自定義選中與未選中樣式,以及菜單項根據所在位置設置不同的背景圖(首項圓角、末項圓角、中間項直角)。

整體佈局

<LinearLayout 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:background="#DFE1E0"
    android:orientation="vertical">

    <LinearLayout
        style="@style/SettingItemTop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            style="@style/MySettingText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="開啟通知" />

        <CheckBox
            style="@style/MyCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        style="@style/SettingItemMiddle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            style="@style/MySettingText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="開啟鈴聲" />

        <CheckBox
            style="@style/MyCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        style="@style/SettingItemBottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            style="@style/MySettingText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="開啟震動" />

        <CheckBox
            style="@style/MyCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

為什麼要用 style

注意這一句:style="@style/MySettingText"。這裡用 style.xml 來控制樣式。

Android 實際開發中,同一個部件的屬性組合會反覆出現。如果每個控件都手工輸入一遍這些屬性,不僅低效,後續修改時也容易漏改。把重複的屬性集合定義成一個樣式,用到時引用即可。

style="@style/MySettingText" 控制文字的屬性,代碼如下:

<style name="MySettingText">
    <item name="android:layout_margin">10dp</item>
    <item name="android:layout_weight">6</item>
</style>

這裡只用了兩個屬性,開發中可按實際需要自行增減。以後每個設置項的文字不必再重複添加,引用一下就行。

這裡的 layout_weight=6 配合 CheckBox 的 layout_weight=1,實現了「文字佔六份、開關佔一份」的比例佈局。這種用權重而非固定 dp 的做法,能在不同屏幕密度下保持一致的比例關係,比寫死寬度健壯得多。

CheckBox 樣式自定義:selector

先看 CheckBox 引用的樣式:

<style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">
    <item name="android:button">@drawable/check</item>
    <item name="android:layout_margin">10dp</item>
    <item name="android:layout_weight">1</item>
</style>

注意 parent 屬性:它繼承了系統的 CheckBox 默認樣式,我們只覆寫需要改的三項。這是樣式繼承的標準用法,比從零定義更安全。

關鍵是這一句:

<item name="android:button">@drawable/check</item>

它把 CheckBox 的按鈕部分替換成自定義的 check 資源,而這個資源使用了 selector。

在 res/drawable 下建立名為 check.xml 的文件:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:state_checked="true"
          android:drawable="@drawable/on" />
    <item android:state_checked="false"
          android:drawable="@drawable/off" />
</selector>

其中 @drawable/on 是選中時顯示的圖像,@drawable/off 反之。

selector 的更多能力

state_checked 只是 selector 的一種狀態判據。實際開發中常用的還包括:

  • android:state_pressed:按壓狀態,做按鈕點擊回饋的首選;
  • android:state_enabled:控件是否可用,做置灰效果;
  • android:state_focused:焦點狀態;
  • android:state_selected:選中狀態,常用於列表項高亮。

selector 的 item 是有順序的:系統從上往下匹配,第一個滿足的即生效。所以更通用的條件要放在後面,更具體的放在前面,否則會被提前匹配掉。

另外,selector 裡除了 android:drawable 指定圖片,還可以用 android:color 做顏色變化,或者嵌套 shape 定義圓角、漸變、描邊等形狀,足以應付絕大多數界面裝飾需求。

首項與末項的圓角背景

菜單第一項上方圓角、中間項四角皆方、最後一項下方圓角,這個效果是通過給三個 LinearLayout 分別設置不同的背景圖片實現的。

除了切圖,也可以用 shape drawable 實現同樣效果:定義三個不同的 shape 文件,只在上兩個角或下兩個角設置 <corners android:topLeftRadius="10dp" ... />,其餘角設為 0。這種方式無需切圖、支持任意尺寸拉伸,是更推薦的做法。

不過要注意,如果列表項數量會動態變化,硬編碼「首項、中間項、末項」三種樣式就不合適了,此時應該在 Adapter 中根據 position 動態切換背景資源。

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