MD-DrawerLayout

DrawerLayout充當窗口內容的頂級容器,允許從窗口的一個或兩個垂直邊緣拉出交互式“抽屜”視圖
DrawerLayout分爲側邊菜單和主內容區兩部分,側邊菜單可以根據手勢展開與隱藏,主內容區的部分可以隨着菜單的點擊而變化。

官方的DrawerLayout是這麼寫的

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!--一個NavigationView由一個header和一個main_drawer組成-->
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

使用DrawerLayout要注意幾點:

  1. 主內容視圖一定要是DrawerLayout的第一個子視圖。 就如官方代碼的那樣,app_bar_main是主內容視圖,那麼他一定要放到代碼的第一個視圖才行,放到前面。

  2. 主內容視圖寬度和高度需要match_parent

  3. 必須顯示指定側滑視圖的android:layout_gravity屬性 android:layout_gravity = "start"時,從左向右滑出菜單 android:layout_gravity = "end"時,從右向左滑出菜單 不推薦使用left和right!!!

  4. 設置側滑事件:mDrawerLayout.setDrawerListener(DrawerLayout.DrawerListener);

  5. 要說一點:可以結合Actionbar使用當用戶點擊Actionbar上的應用圖標,彈出側滑菜單! 這裏就要通過ActionBarDrawerToggle,它是DrawerLayout.DrawerListener的具體實現類, 我們可以重寫ActionBarDrawerToggle的onDrawerOpened()和onDrawerClosed()以監聽抽屜拉出 或隱藏事件!

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();
    
  6. 如何隱藏 那個按鈕(點擊它滑出側滑欄),將上面代碼中的toolbar刪掉, 他的默認是,只要你將toobar綁定到 drawer,那麼你的toobar就會出現那個按鈕。

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();
    
  7. 還有很多自定義的屬性,詳細設置。

  8. 這個DrawerLayout一定要配合Navigation嗎?
    不!其實那個navigationView就是一個導航頁面,由官方佈局,命名,我們能用一個ListView來代替他的代碼,實現差不多的功能。

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

    <FrameLayout
        android:id="@+id/ly_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ListView
        android:id="@+id/list_left_drawer"
        android:layout_width="180dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#080808"
        android:choiceMode="singleChoice"
        android:divider="#FFFFFF"
        android:dividerHeight="1dp" />

</android.support.v4.widget.DrawerLayout>

所以,這個NavigationView只是一種佈局格式,上面有header,下面是menu,看起來好看而已。

問題:一般應用首頁,都會有多個fragment(要求每個fragment都有抽屜佈局),底部會有導航按鈕。那麼彈出抽屜佈局的話,下面是不會被遮擋住的,這樣拉出菜單底下按鈕還可以點擊切換。

解決方法:https://www.jianshu.com/p/a403d447a036 嘗試一下,或者抽屜佈局放在首頁activity中。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章