編輯:Android 開發實例
近來在研究 Launcher 的過程中,我注意到 SlidingDrawer 這個類別,也就是我們常說的「抽屜」。它正是桌面下方那塊可以往上拉出應用列表的元件。
用法其實不難,關鍵是理解它必須同時具備兩個子元件:
按下把手,抽屜要嘛拉開、要嘛收起,就這麼單純。以下拆解實作步驟。
新建 Android 專案,命名為 SlidingDrawer。圖示素材我是直接取自 Launcher2,放進 drawable-hdpi 目錄即可。把手用到的三張狀態圖分別是 handle_normal、handle_pressed、handle_focused。
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 的源碼會很有收穫,那裡對手把的觸控處理與動畫細節做得相當細緻。
Android 登入畫面開發實例解析
登入畫面是許多需要帳號的應用的入門關卡,透過詢問使用者帳號與密碼來完成驗證,無論是 Facebook、微博還是各類服務都適用。 本章將說明如何打造登入介面,並示範如何處理
Android 本地化入門:靠資源資料夾自動切換語言
你的 App 可能同時被台灣、日本、德國的使用者開啟。如果介面永遠只有一種語言,對不諳該語言的人來說就是一道門檻。本地化要處理的範圍包括文字、數字格式、日期寫法,乃至於圖
Android JSON 解析器完整實作
JSON 是 JavaScript Object Notation 的縮寫,它是一種與語言無關的資料交換格式,被廣泛視為 XML 的理想替代品。本章將說明如何在 Andr
Android ImageSwitcher:為圖片切換加上轉場動畫
直接讓圖片「啪」地出現在螢幕上,觀感總是略顯生硬。若能在切換的瞬間加上淡入、滑入這類轉場動畫,介面就會流暢許多。Android 為此提供了 ImageSwitcher 元