DrawerLayout要求ContentView(即主页内容部分)必须是DrawerLayout的第一个Child View,左侧滑动出现/隐藏的MenuView(例如NavigationView)必须是DrawerLayout的最后一个Child View。
<?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/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
....
</RelativeLayout>
<-- 放在最后 -->
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/header_layout"
app:menu="@menu/main" />
</android.support.v4.widget.DrawerLayout>
并且需要给滑动出现MenuView添加android:layout_gravity=”start”属性。如果滑动出现的Child View不是最后一个Child View,那么将出现不能被滑动收起的情况。
android:layout_gravity="start"