Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 高手進階教程(二):SlidingDrawer 抽屜效果實作

編輯:Android 開發實例

近來在研究 Launcher 的過程中,我注意到 SlidingDrawer 這個類別,也就是我們常說的「抽屜」。它正是桌面下方那塊可以往上拉出應用列表的元件。

用法其實不難,關鍵是理解它必須同時具備兩個子元件:

  • handle(把手):使用者點擊或拖曳的目標。
  • content(內容):抽屜拉開後真正顯示的區塊。

按下把手,抽屜要嘛拉開、要嘛收起,就這麼單純。以下拆解實作步驟。

步驟一:建立專案與準備素材

新建 Android 專案,命名為 SlidingDrawer。圖示素材我是直接取自 Launcher2,放進 drawable-hdpi 目錄即可。把手用到的三張狀態圖分別是 handle_normal、handle_pressed、handle_focused。

步驟二:撰寫 main.xml 布局

SlidingDrawer 必須透過 android:handle 與 android:content 兩個屬性,分別指向自己內部哪個子 View 充當把手、哪個充當內容:

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

    <SlidingDrawer
        android:id="@+id/slidingdrawer"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:handle="@+id/handle"
        android:content="@+id/content">

        <Button
            android:id="@+id/handle"
            android:layout_width="88dip"
            android:layout_height="44dip"
            android:background="@drawable/handle"
            />

        <LinearLayout
            android:id="@+id/content"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#00ff00">

            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button"
                />

            <EditText
                android:id="@+id/editText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />

        </LinearLayout>

    </SlidingDrawer>

</LinearLayout>

幾點提醒:

  • android:handle 與 android:content 的值必須是 SlidingDrawer 直接子節點的 id,寫成外部 View 的 id 是不會生效的。
  • 這裡用 dip 而非 px 設定把手尺寸,這樣在不同解析度螢幕上才不會失真。
  • SlidingDrawer 只能有一個 handle 與一個 content,且建議作為布局中唯一的直接子元件。

步驟三:為把手加上狀態樣式

光放一張圖有點單調,我們可以用 selector 讓把手依狀態切換圖形。在 drawable 目錄新增 handle.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/handle_normal" />
    <item android:state_pressed="true"
        android:drawable="@drawable/handle_pressed" />
    <item android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/handle_focused" />
    <item android:state_enabled="true"
        android:drawable="@drawable/handle_normal" />
    <item android:state_focused="true"
        android:drawable="@drawable/handle_focused" />
</selector>

這份 selector 依序判斷「視窗未聚焦」「被按下」「取得焦點」「一般啟用狀態」,各自對應不同圖檔。回頭把布局裡手把的 android:background 由 @drawable/handle 指向這個 selector,就能得到有回饋感的按壓效果。

步驟四:執行檢視

執行專案,畫面會是一片灰色底、中央偏下有一個把手。點一下把手,綠色的內容區塊就會滑出,裡頭躺著一個 Button 和一個 EditText;再點一次,抽屜收回。

補充:幾個實務上的注意事項

這個範例算是入門等級,若要應用在正式專案,還有幾件事值得留意:

  • 預設開合狀態可以透過 android:allowSingleTap、android:animateOnClick 等屬性微調。
  • 方向由父容器與 android:orientation 決定,垂直抽屜(由上往下)與水平抽屜(由左而右)寫法略有不同。
  • SlidingDrawer 在較新的 Android 版本中已被標示為 deprecated,官方建議改用 NavigationView、DrawerLayout 或 Bottom Sheet 等方案。理解它的原理仍有價值,但新專案請優先考慮標準元件。

想更深入的話,直接翻 Launcher 的源碼會很有收穫,那裡對手把的觸控處理與動畫細節做得相當細緻。

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