編輯:關於 Android 程式設計
側滑抽屜式菜單現在幾乎是應用標配:不用跳頁、不用按硬件菜單鍵,點一下按鈕菜單就滑出來,而且能放進去不少內容。本文以官方 DrawerLayout 為例,把它的結構梳理一遍。

DrawerLayout 位於 Support Library 中,使用時需要引入 android-support-v4.jar,並在代碼裡 import android.support.v4.widget.DrawerLayout;。
如果找不到這個類,先用 SDK Manager 更新 Android Support Library,然後到 Android SDK\extras\android\support\v4 路徑下找到 android-support-v4.jar,複製到專案的 libs 目錄並 Add to Build Path。
新建 Android 專案時,也可以直接選擇使用 Navigation Drawer 模板:

抽屜本質上是一個 ListView,這個 Fragment 負責把它 inflate 出來、綁定點擊監聽、掛上適配器:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//給抽屜 ListView 找到對應的 XML 佈局
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
//綁定點擊監聽器,點擊時選中對應項
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectItem(position);
}
});
//綁定適配器
mDrawerListView.setAdapter(new ArrayAdapter<String>(
getActionBar().getThemedContext(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
new String[]{
getString(R.string.title_section1),
getString(R.string.title_section2),
getString(R.string.title_section3),
getString(R.string.title_section4),
getString(R.string.title_section5),
}));
//設置抽屜 ListView 以顯示某一選中項的形態出現
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}
這裡用 choiceMode="singleChoice" 讓列表具備單選效果,並自訂分隔線顏色與高度:
<ListView 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="@color/image_bg_green"
android:choiceMode="singleChoice"
android:divider="@color/image_bg_lightgreen"
android:dividerHeight="1dp"
tools:context="net.nowamagic.magicapp_v7.NavigationDrawerFragment" />
抽屜裡的每一格由一個相對佈局填充,內容就是一個 TextView:
<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="net.nowamagic.magicapp_v7.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
基於 Navigation Drawer 模板新建的專案,跑起來大致如下:

總結一下結構關係:DrawerLayout 作為容器,第一個子 View 是主內容區,第二個子 View 是抽屜(這裡是 ListView),而 Fragment 負責填充抽屜的數據與互動。理解這個層級之後,替換成自定義適配器或加上頭部背景,都是水到渠成的事。
Android 顏色選擇器與文字換色實作教學
做個能即時改字色的功能,需要一個好用的顏色選擇器。下面從「一張七彩圖」開始,一路做到可套用到 TextView 的 ColorPickerDialog。 一、用 Lin
Android 繪製 QQ 新用戶注冊界面完整實作
延續前一篇登入界面的做法,這次來做注冊頁。先看成果: 這個畫面的重點有三處:Spinner 下拉列表的使用、標題欄的處理方式,以及按鈕的 enable 狀態切換。
Android 網路連線處理方式整理
在 Android 中,常見的網路連線方式包括使用 URLConnection 或 HttpURLConnection、使用 HttpClient,以及透過 WebVie
Android 事件傳遞機制解析
Android 的畫面由 Activity 與 View 組成,而使用者操作通常會轉換成觸摸事件。要做出流暢且可預期的互動,就必須理解事件如何從系統進入 View,再往上