代码已经分享至github:https://github.com/YanYoJun/NavigationDemo

转载请注明原文链接:http://www.cnblogs.com/yanyojun/p/8076015.html

一、直接看布局

<?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" />
 
     <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中嵌套一个NavigationView就可以了

这里有一个fitSystemWindow,这句话总结的很好:

fitsSystemWindow 默认是true,就是组件都在屏幕内,但是不包括statusBar。设置成false后,整个屏幕都可以放置组件,没有statusBar和window之分。