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

DrawerLayout 側滑菜單導航實踐:原生 Android 設計風格的回歸

編輯:關於 Android 程式設計

DrawerLayout 推出已經有一段時間,但它在國內 App 中的普及度並不算高。這背後有個值得反思的現象:不少產品在設計上習慣性地模仿 iOS 風格,反而忽略了 Android 本身已經有一套成熟且優雅的導航語言。側滑菜單正是其中代表,這次我在專案中爭取把它落地了。

版面配置

DrawerLayout 位於 v4 套件庫中,使用時只需在 XML 裡描述結構:根節點是 DrawerLayout,第一個子 View 是內容區,第二個子 View 是側滑菜單,並透過 layout_gravity="start" 指定它從哪一側滑出。

<android.support.v4.widget.DrawerLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:orientation="vertical">
        <!-- 內容區 -->
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#ffffff"
        android:orientation="vertical">
        <!-- 側滑菜單 -->
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

ActionBarDrawerToggle:讓漢堡圖標活起來

ActionBarDrawerToggle 負責控制 DrawerLayout 的顯示與隱藏,並把 ActionBar 左上的漢堡圖標與抽屜狀態綁定在一起——點擊圖標可以開關菜單,菜單開啟時圖標會變形成返回箭頭。

使用時需要在 Activity 的三個位置處理它:

onCreate 中建立實例並註冊監聽;onPostCreate 中呼叫 syncState() 完成狀態同步;另外在 onConfigurationChanged 與 onOptionsItemSelected 中把事件轉發給 toggle。

public class DrawerActivity extends AppCompatActivity {
    DrawerLayout mDrawerLayout;
    ActionBarDrawerToggle toggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_activity);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        toggle = new ActionBarDrawerToggle(DrawerActivity.this, mDrawerLayout,
                R.string.hello_world, R.string.hello_world);
        mDrawerLayout.setDrawerListener(toggle);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("TestDrawerLayout");
    }

    @Override
    public void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        toggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        toggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (toggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

syncState() 的作用是把 toggle 中的圖標狀態與 DrawerLayout 的實際狀態對齊,並將抽屜圖標設定為 ActionBar 的 Home 按鈕圖示。少了這一句,旋轉螢幕後圖標狀態就會與菜單狀態不一致。

三個實務注意點

1. DrawerLayout 的類別路徑是 android.support.v4.widget.DrawerLayout,別找錯套件。

2. 主題必須顯示 ActionBar,例如 Theme.AppCompat.Light.DarkActionBar,否則 getSupportActionBar() 會回傳 null。

3. ActionBarDrawerToggle 本身就是 DrawerListener 的實作,如果標準行為不合用,你可以自己實作 DrawerListener 來定制開合動畫或攔截滑手勢。

寫完之後最大的感受是:Android 原生的設計語言本身已經足夠成熟,真正需要改變的往往不是技術能力,而是設計決策上的慣性。

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