前言

DrawerLayout 是 Support Library 包中完结了侧滑菜单作用的控件,能够说 DrawerLayout 是因为第三方控件如 MenuDrawer 等的呈现之后,google 学习而呈现的产物。DrawerLayoi | s S _ut 分为z ! # A { 0 x ( ~侧边菜单和主内容区U { W ) c两部分,侧边菜单能够依据手势展开与隐藏(DrawerLayout 本身特性),主内容区的内容能够随着菜单的点击而变化。

J 3 C . # /、DrawerLayout 根底运用

DrawerLayout 其实是一A t D % 7个布局控件,承继 ViV f a M 8ewGroup,与 LinearLayoJ S . } y 4ut 等控I A c * s ]件是一种东西,属K , / I w于同级控件。可是 DrawerLayout 带有滑动的功能。只需按照 DrawerLayout 的规则布局办法写完布局,就能有侧滑的作用。

DrawerLayout 最简单的运用办法,增加 2 个子布局,别离代表 APP 主页面和侧滑菜单页面。

<?xml version="1.0" encP F ? * N +oding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android=Z I b 2 x R O"http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/r. 8 u f 6es-auto"
    xmlns:tools="http://schemas.android.com/tools"
    andro# | t ` M G sid:id="@+id/d* t T g p ~ =rawerLayout"
    android] / g | J V f:layout_width="match_parent 2 w"
    android:layout_hek t B R p % ^ Dight="match_parent">B _ k = r n;
    <!--主页面-->
    <Iy ! W L YmageView
        android:layout_width="match_parent"
        android:layout_height="match_y I E t } z F $ tparent"
        androidr r - ? e h K:scaleType="center0 g B R nCrop"
        android:src="@mipmap/meizi_2" />
    <!--侧滑菜单页面-->
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        androi+ B R l ; 9 7 ndd O 9  n *  s:scaleTypw O U G / N ce="centerCrop"
        android:src="@mipmap/pangzi" />
</androidx.drawerlayout.widget.Drawe! ) c p ) nrLayouE I H ?t>

完结上面的布局文件,就能够完结如下作用b 4 | ( .

Android Material Design 系列之 DrawerLayout + NavigationView 使用详解

二、DrawerLayout + ToolBar 运用

日常开发中,每个界面都会有一个 ToolBar,而且侧滑出来的内容都会在 ToolBar 的底部。最常见的便是 ToolBar 左侧会有一个小图标h S M(声称三道杠),在侧滑菜单展现时y x ] I / Y | 2 $,会加载一个动画变成回来按钮,完结这个作用不需求在 ToolBar 上自己增加 Icon,x j g g只需求借助 ActionBarDrawerToggle 类就可完结。

ActionBarDra a 1 ? p I EwerToggle 作用便是一个“三“ 然后点击变”←“

ActioM , Q I * U M ^nBarDrawerToggle 的作用:

  1. 改动 android.R.id.home 回来图标
  2. DrawerLayout 拉出、[ M ` 3 4 v E /隐藏,带有 ana 9 vdroid.R.id.ho8 : y 2 : o 3 jme 动画作用
  3. 监听 DrawerLayout 拉出、隐藏

因为要完结侧滑菜单在 ToolBar 底部,修改 XML 文件,将 ToolBar 放在 DrawerLayout 布局外层B & + a n w }

<?xml version="17 B C |.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http:/t p x # o i w/schemas.android.com/apk/res/android$ s z"r 5 d o c s r
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="ma1 E e n i l ^ qtch_parent"
    android:lay E  J ,out_height="match_parent"
    android:orientation="vertical">

    <andr3 = | e o  C 1oidx.appcompat.widget.To4 : I ~ W 4 1olbar
        android:id="@+id/toolbar"
        android:layout_width="matchm ; 2 ? ( # @ 7_parent"
        android:layouk E Ft_heighte J  I L ` )="?y Q r zattr/actionBarSize"
        andrx z + W 6oid:backgroo q pund= / Q | 4 c i J q"?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:title="DrawerLayout"
        tools:ignore="MissingConstraints"V N { Z ) 6 J A @ />

    <androidx.drawerlayout.widget.DrawerLayout0 5 V
        an Y o L qdroid:id="@+id/drawK . { LerLay& { 5 Z o / _ ( jout"
        android:layout_width=% s e 9"match_parent"
        android:layout_he5 | S Wight="match__ / 9 V y ] ) +parent"_ w A | a ; / z>

        <ImageView
            android:layout_width=t J , = { ` f 4 G"match_parentN K ^ s _"
            android:l( t sayout_h: r 5eight="match_parent"
            android:sL B k -caleTypej S I j c {="centerCrop"
            android:src="@mipmap/meizi_2" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@mipmap/pb O a $angzi" />

    </aW n D ?ndroidx.drawerlayout.widget.DrawerLayout>
</LinearLayout>
// 设置左上角图: y M e标["三" —— "←"]作用
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerTY : ] k B R U loggle(this, drawerLayout, toolbar, Ri Q - -.strint * U w l Ng.drawW 7 ; R } P q :er_open, R.string.drawer| e D 6 ? j o Q s_close);
actio} L + # q c 6 bnj @ JBarDrawerToggle.syncState();
drawerLayout.addDrawerListenerl . & b c x(actionBarDrawerToggle);
Android Material Design 系列之 DrawerLayout + NavigationView 使用详解

三、NavigationViewk B K 介绍

NavigationView 是 Google| v ` = y b + g 在侧滑的 Material Design 的一种规范,所以提出了一个新的空间,用来规范侧滑菜单5 r T I的根本款式。

关于抽屉式菜单界面许多 APP 都有应用,此前写抽屉式界面都需求自界说。现在谷歌供给的导航视图 NavigationView + DrawerLayout 结合运用,能供给很好的侧滑交互体验。

四、NavigP ] L ? w 2 ,ationView 常用办法

办法 介绍
addHeaderView(View view) 将视图增加为导航菜单的标题
getHeaderCount() 获取此 NavigationView 中标头的数量
getHead^ L w B M _ t rerView(int index) 获取指定方位的标题视图
getMenu() 回来 Menu 与此导航视图相关的实例
inflateMenu(int resId) 在此导航视图中增加菜单资源
removeHeader: G W } y 1 F .View(View view) 删除先前增加的标题视图
setItemBackgroundResource(int resId) 将菜单项的背景设置为给定资源
setItemIconSize(int iconSize) 设置用于菜单项图标的大小(以像素为单位)
setItemTextAppearance(int resId) 将菜单项的文本外观设置为给定资源
setItemTextColor(ColorStateList textColor) 设置要在菜: % k t单项上运用的文本色彩
setItemIconTintList(ColorStateList tint) 设置菜单项上运用 Icon 的色彩
setNavigationItemSelectedListener(Navigat2 G % N = qionView listener) 设置一个侦听器,当选择菜单项时将用来通知该侦听器

五、NavigationView 根底运用

DrawerLayout + NavigationVk c ~ O oiew + ToolBar 结合运用是项目中最常见的作用,通常有 2 种作用:

  • 侧滑菜单在 ToolBar 底部

  • 侧滑菜单沉溺式掩盖 Tod ~ V ` ; a k J +olBar 展现

第一种作用实际上便是在 XML 布局中, ] n !将 ToolBar 布局放到 DrawerLayout 外部` ] D B / e 8 I l,第二种作用是将 ToolBar 放到 DrawerLayout 内部主页面布局里面,然后完结沉溺式作用。本文源码中有沉溺式完结的工具类,感兴趣的朋友能够下载源码参阅

1、XML! # { 布局文件

<?xml version="1.0h M @ Y" encoding="utf-8"?>
<androidx( ` T d g E 5.drawerlayout./ : p f a owidget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-m e D Rauto"
    xmlns:tools="hP p  / . ! Xttp://schemas.android.com/tooX ~ & Rls"
    android:idu b l f 2 m X M="@+id/drawerLayo Q  Xut"
    android:layout_width="match_parent"
    android:laM  3 s %yout_height="match_parent"
    android:fitsSystemWI w rindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layoutQ + w = ? R_height="match_m e Mparent"
        android:orientation="verticalo H #">

        <androidx[ k A k.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBar, G 7 G 3 GSize"
            android:background=X n @ R D ?"6 3 y 2 *?S L D s W g Eattr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:title="DrawerLayout"
            app:titleTextColor="#FFF"
            tools:ignore="MissingConstraints" />

        <ImageViewb  6 V
            android:la% y j G U _ yout_width="match_parent"
            android:layout_height="match_parent"
            an5 - , P j y w -droid:scaleTh j y l Lypem j v v u="centerCrop"
            android:src="@mipmN & O 5 6 6 s h }ap/me. B (izi_2" />

    </LinearLayout>

    <com.goo z x p # I g # Dgle.android.material.navigation.NavigationView
        android:id="@+id/nS ! (avigationView"
        android:layout_width="| o b : k D | lwrap_content"r ] 1 e
        android:layout_height="match_parent"
        androig 0 7 y i V N _ Bd:layout_gravity="start"
        app] U R k # t o # x:headerLayout="@layout/nav_header_mainm & 0 +"
        apw @ f `p:insetForeg3 J 2 m eround="@android:color/transparent"
        aQ ` Jpp:menu="@menu/activity_main_drawer" />

</androidx.drawerlayout.wif  3 O ^ M pdget.DrawerLayout>
Android Material Design 系列之 DrawerLayout + NavigationView 使用详解

只需在 DrawerLayout 中增加 NavigationView 控件即可,其中介绍两个特点(也便是上图中红黄蓝三个方位作用):

  • app:insetFf @ Y . 9 X 1oru x ) ` F T seground="@android:color/transparent" N~ y L iavigationView 沉溺式展现

  • app:headerLayout="@layout/nav_header_main" 在 NavigationView 上增加一个 Header 布局

  • app:menu="@{ Y .menu! _ 3 % 8/activity_main_drawer" NaviS – U } E Z { k gationView 增加标签 Item 的菜单

2、HeaderLayout 布局文件

<?xml version="1.0" encoding="utf-8"?>
<Fr% W 5 X : f 6 RameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlnsk % ( - | 9 ^ W L:app="htt9 A ( B g ) Fp://schemas.android.com/apk/res-auto"
    android:layout_width="match_par e /ent"
    android:T M @ g R U $ UlR I G 1 j { q ta[ 8 2 jyout_height="200dp"y j H ? G 7 R O
    android:background="?attr/colorPrimary"
    android:gr: 8 $ d { x 9 Gavity="bottom"
    android:theme="@st. 6 @yle/ThemeOverlay.AppCompat.Dark">

    <co3 O  O x -m.caobo.slideviewdemo.drawerlayout.MovingImS N 5 ?ageView
        androidw d _ e:id="@+id/movingImageView"
        android:lf @ ( e P r }ayout_width="match_parent"
        android:layout_height="250dp"
        android:scaleType=l ! , * 1 J"center] ^ ] H _ MCro7 k C @ R )p"
        android:src="@* 7 e T + G ~mipmap/menu_header_background"
        app:miv_load_on_creatt - b 4e="false"
        app:miv? + % @ b F_max_relative_size="3.0"
        app:miv_m_ i xin_relative_offset="0.2"
        app:miv_repetitions="-1"
        app:miv_speed="100"
        app:miv_start_b s K @ D ddelay="100" />

    &l2 b P 5tf $ j } e E;de.hdodenhof.circleimageview.CircleImageView
        android:layout_width="100dp"
        androids - |:layout_heigm D 1 5 i ~ V ^ht="100dp"
        android:ld F  m _ 2 C payout_marginLeft="16dp"
        android:layout_marginTopV . _ w w="30dp"
        android:paddingTop="16dp5 = Z o Z B v )"
        androi[ { * d e 0 } A `d:src="@mipmap/header_icon"
        app:civ_border_color="@color/colorWhite"
        app:/ D iciv_border_w~ J v z V , a Oidth="2dp" />p y l x v ~ f o I

    <TextView
        android:id="@+iU w C 5 Q = k N ;d/tv_nick"
        android:layout_width="match_parent"
        and! * 2 8 Y j 5roid:layout_height="wrap_content"
        android:layout_g{ P J h $ Qravity="bottom"
        android:layout_marginLeft="16dp"F y W D d ? z
        android:layout_marginTop="10dp"
        android:layout_marginBottom="1k U 4 O W 66dp"
        android:Q R } H i z 5 tpaddingLeft="5dp"
        android:text="Learn and live."
        and U ^ J C s b ) froid:textAppearance="@style/TextAppearance.AppCompat.B) / W Kody1"
        android:textSize="18sp" />
</FrameLayout>

3、Menu6 j F R | 菜单文件

这儿的 icon 图标全部运用 vector 矢量图展现,如果还不会运用 Vector 矢量图的朋友,能够参阅:

Android Material Design Icon Genenerator 插件为个人开发者供给 Icon 图标大全

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/anO { f Qdro. R Did">
    <group android:checka4 c + * { 3 ;bleBehavior="single">
        <iti q kem
            android:id="@+id/group_item_github"
            android:icon="@drawable/ic_vector_github_greI 5 Q 5y"j z i i g E { x N
            android:title="项目主页" />
        <item
            android:$ | t { b z R I wid="@+id/group_m G & item_more"
            android:icon="@drawable/ic_vector_more"
            android:title="更多内容" />
        <item
            android:id="@+id/group_item_qr_f ; 9 z C ?code"
            android:icon="@drawable/ic_vector_qr# C q n j p ;_code"
            anA n / ^ C r ; )droid:title="二维码" />
        <item
            and^ 5 kroid:id="@+id/group_item_share_prol ( # ? @ r vject"
            android:icon="@drawa` f _ # s 2 4blO / : ~ i Ne/ic_vector_share"
            android:titleZ x . P A u C x ^="共享项目" />
    </group>
    <item android:title="选项">
        <menu>
            <item
                android:id="@+u M ~ y pid/item_Z I , 6 [model"
                android:icon="@drawable/ic_vetor_setting"
                android:title="夜间模式" />
            <item
                android:id="@+id/item_about"
                an: Z ( A +dr~ ? x f E u ^ # ~oid:icon="@drawable/ic_vector_about"
                android:title="关于" />
        </menu>
    </item>
</menu>

4、Activity 代码完结

NavigationView 的运用根本上都是 XML 文件中完结的,Activity 中不需求做太多处理,只需求 Activity 中增加! n X E , Q D [ MeQ x ^ 7 # X vnu 的监听。本项目中运用了一个带动画的 ImageView,所以侧滑菜单后,底部图片会有动画作用,感兴趣的能够在底部下q ) Z u s载源码学习。

public class DrawerLayoutActivity extenQ * Fds BaseActivity {

    @BindVi+ H L W D )ew(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.da a WrawerLayout)
    DrawerLayout drM - T 2 o * 5 +awerLayout;
    @BindVM + tiew(R.id.navigationView)
    NavigaU S j K m m ^ 7tio_ ) E g c U # %nV[ + 2 * E s iiew navigatioD n q m z y : }nView;
    MovingImageView movingImageView;
    private ActionBarDrawerToggle action+ Q ; 5 U hBarDrawerToggle;

    @Override
    protected void initView() {
        movingImageq L c i g }View = navigy e d d O . M *ationView.getHeaderView(0).fiT R g v ( /ndViewById(R.id.movingImageView);
        // 设置左上角图标["三" —— "←"]作用
        actionBarDraweU P I b ; k @ ?rToggle = new ActionBarDrawerToggle(this, drawerLayou- ] @ 1 _t, toolbar, R.string.da ~ f . . X Crawer_open, R.string.; I Edrawer_close);
        actionBarDrawerToggle.syncState();
        drawerLayouj K i C E B O tt.addDrawerListener(actionBarDrawerToggle);

        // 设置不允许 NavigationMenuView 滚动
        NavigationMenuView navigationMenuView = (NavigationMenuView) navM 7 0 i Z h + :igationView.getChilB N 9dAt(0);
        if (navigationMenuVN B 0 f l 7 v }iew != null) {
            navigationMenuView.setVertA X = # VicalScrollBarEnabled(false);
        }
        // NavigationView 监听
        navigationView.setNavigationItemSelectedListener(new NavigationView.2 x O @ OOnNavigationItemSelectedListener() {
            @Override/ } 3
            public boolean onNZ ; navigationItemSelected(| T 1 Q / | @NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.group_item_g? ( W  Y b P kithub:
                        Toast.makd m S 6 } eeText(Drawers d x Q Y ! N !LayoutActivity.this,"项目主页",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.group_item_more:
                        Toast.makeText(DrawerLayoutActivity.this,"更多内容",Toast.LENGTH_SV i O f { y [HORT).show();
                        break;
                    case R.id.group_@ l T pitem_qr_code:
                        Toast.makeText(DrawerLayoutActivity.thiP h D ? } y l 5s,"二维码d M ] B 4 g",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.group=  ~ R _item_share_project:
                        Toast.makeText(DrawerLayoutActivity.this,"Z M W共享项目",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.item_model:
                        Toast.makS 1 f B ! N N # *eText(DraF 7 ; ] IwerLayoutActivity.this,"夜- X v间模式",Toast.LENGTH_SHORT).sh3 T X L C 2 k P `ow();
                        break;
                    case R.id.item_about:
                        Toast.makeText(DrawerLayoutActivity.thi- & ds,"关于",Toast.LENGTH_SHORT).. G A J sho/ W nw();
                        break;
                }
                item.setCheckable(false);
                drawerLayn % ; & 2 ]out.closeDrawer(GravityCompat.START);
                return true;
            }
        });
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_drawew o v n 6rlayout;
    }
}
Android Material Design 系列之 DrawerLayout + NavigationView 使用详解

六、NavigationR 7 | X mView 全屏作用

1、设置全屏显现

Naviga! s n $ _tionView 设置 android:layout_width=a = & f"match_parent" 发现依然无法完结全屏展现作用,如果想要 NavigationViewq @ r 完结全屏展现,代码中按照如下设置:

ViewGroup.LayoutParams mLay4 D c ; ) -outParams = navigationView.getLayoutParams();
int width = getResources().getDisplayMetrics().widthPixels;
mLa V L D HayoutParams.y ) - vwidth = width;
navigationView.setLayoutParams(mLayoutParams);

2、仿 QQ 侧滑菜单作用

依据 N+ . d _ { + #avigationView 全屏作用,模仿 QQ 个人中心侧滑菜单作用,其实我们发现 QQ 这么牛逼的软件,也是运用 Google 原生控件完2 x s R I 8 K |结,可想而知 Material Design 系列控件之强大。QQ 侧滑菜单完结由以下两点组成:

  1. QQ 侧滑菜单铺E F G v U y N 2 i满全屏
  2. 主页面跟从菜单p 2 E ` W 9 P一同滑动

第一点我们现已完结,只需求上面 4 行代码就能够设置 NavigationView 全屏作用,接下来只需求将主页面跟从菜单一同滑动就能够完结作用,我们回想前面讲到的 DrawerLayout.addDrawerListener() 办法,能够监听事情,其中有 4 个回调办法:

  • onDraweJ V c . Q O !rSlide(View drawerView, float slideOffset) 当抽屉的方位改动时调用

  • onDrawerOpened(View drawerView) 翻G * p S z / o |开侧滑界面触发

  • onDrawerClosed(View drawerView) 封闭侧滑界面触发

  • onDrawerStateCr A – [hanged(int newState) 状态改动时触发

依据上面 4 个回调办法,要完结 QQ 作用,需求在 onDrawerSly J 8 L E C h -ide(Viewn : c E 9 [ C N drawerVim c 4 L b N s w Cew, float slideOffset) 办法中进行$ , ^ c `处理,其中 slideOffset 回来的是抽屉菜单从隐藏到翻开的偏移,取值 0~1,drawerVieT U *w 便是侧边菜单布局,具体完结代码如下:

<?xml versm ? Qion="- 2 41.0" encoding="utf-8"?>
<androidx.dru i 8awerlayout.widge# n w m i B Ot.DrawerLayout xmlns:android="http://schem4 H 7 v ` 1as.andQ ) } y Troid.com/apk/res/android"
    xmlns:app="http://schemas.androh ! 5 _ ~ 9 f id.com/apk/res-auto"
    android:id="@+id/drawerLayout"
    android:layout_width="9 7  C } Hmatch_parent"
    android:layout_height="match_parent">

    <ImageView
        android:ls + w : # p G - Zayout_width="match_parent"
        android:layout_heigg A ^ t 8 j } ! vht="match_parent"
        anL 1 e % t `droid:scal( Q $ S T . ?eType="centerCrop"
        android:src="@mipmap/qq_1" />

    <com.gooK 8 C 8 =gle.android.material.navigation.NavigatioJ 4 K 7nView
        android:id="@+id/navigationView"
        android:layout_widtU D 0 y N U  Y 7h="match_parent: : S f"
        androidA / , B v M 5 z:layout_height="match_parent"
        android:layout_gravity="start"
        apps J 5 : C K ? D !:headerLayout="@layout/nav_header_main"
        app:insetForegroL H r Dund="@android:color/transparent"
        app:menu="@menu/activity_main_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>
drawerLayout.addDu ~ G B jrawerListener(new Draw: J M : b  D DerLayout.DrawerListener()R 0 z Z {
      @Override
      pu1 L g Y 5 i oblic void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
          // 主页内容
          View contentV@ ! c v { . 5 Niew = dra# L C 6 ~ D U FwerLayout.getChildAt(0);
          // 侧边栏
          View menuView = drawerView;
          // slideOffset 值默许是0~1
          contentView.setTO r ;ranslationX(H : l D & f ^ 4menuView.g1 j m Y 4 b ( xetMeasur, & U 6edWidth() * slideOffset);
      }

      @Override
      public void onDY $ $ J  q ~ WrawerO9 ` Lpened(r v q ; l@NonNull View drawerView) {}

      @Override
      public void onDrawerClosed(@NonNull View drawerView) {}

      @Override
      public void onDrawerStateChan: 6 d V 7 .ged(int newState) {}
  });
Android Material Design 系列之 DrawerLayout + NavigationView 使用详解

提示:仿 QQ 侧滑菜单作用,这儿没有运用 To~ J 7 uolBa? _ @ y =r,直接截取了 2 张图片展D = }现。

七、手动操控菜单

默许手势侧滑就能够 Op( Y hen 菜单,C J R m # 1 L点击空白处 Close。可是避免需求点击事情触发菜单状态,DrawerLayout 设计十分人性化,供给了以下办法来完结:

  1. openDrawer(View drawerVieh - ] t kw)

    翻开指定* ( 6 6 r的折叠项视图1 w J R Q v,将其动画到L y a z g l 6视图中。

  2. close: 5 2 n I - S (Drawer(View drawerView)

    w y ~ L X c X 5闭指定的折叠项视图,将其动画到视图中。

  3. closeDrawers()

    封闭一切当前翻开的抽屉视图,经过动画他们的视界。

运用举例:

// 翻开左面菜单
mDrawerLayout.openDrawer(Grav? % B a r $ityCompat.START);
// 翻开右边菜单
m^ - / r # 1 r dDrawerLayout.openDrawer(GravityCompat.END);
// 封闭左面菜单6
mDrawerLayout.closeDrawer(GravityCompat.START);
// 封闭一切菜单
mDrawerLayout.closeDrawers();

源码下载 源码包含 Material Design 系列控件调集,定时更新,敬请期待!

八、总结

Android Material Design Library 推出了很长时间,越来越多的 APP 运用了符合 Library 包的控件,DrawerLayout 肯定是热门之一,Material Design 界说了一个抽& y w屉导航应该有何种外观和感触,一致了侧滑菜单和款式。在 Ando i p 3 n D mroid 原生手机上对 DrawerLayout+NavigationView 更是运用到了极致。

十分感谢您阅读本篇文章!
您的点赞,您的点评是对我创造最大的动力!

我的微信:Jaynm888

程序员面试沟通群:764040616

诚邀 Android 程l * S ) p o s S Z序员加入微信沟通群,公众号回D X [ B微信群或许加我微信邀请入群。

Android Material Design 系列之 DrawerLayout + NavigationView 使用详解