一. 前言
DrawerLayout 是在我们的隐藏区域,也就是我们看不见的屏幕以外,通过手指滑动,将 DrawerLayout 滑倒主屏幕区域。

二. demo
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | <androidx.appcompat.widget.Toolbarandroid:id="@+id/tool_bar"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="#aa77bb"
 android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
 app:popupTheme="@style/Theme.AppCompat.Light"
 app:navigationIcon="@drawable/home"
 />
 
 | 
| 12
 3
 4
 
 | Toolbar toolbar = findViewById(R.id.tool_bar);
 toolbar.setTitle("DrawerLayout");
 setSupportActionBar(toolbar);
 
 | 
2. 添加DrawerLayout
DrawerLayout和ScrollView一样,内部只能嵌套一个布局。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | <androidx.drawerlayout.widget.DrawerLayoutandroid:id="@+id/drawer"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_marginTop="55dp"
 >
 
 <RelativeLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_gravity="left"
 >
 
 </RelativeLayout>
 
 </androidx.drawerlayout.widget.DrawerLayout>
 
 | 
| 12
 3
 
 | DrawerLayout drawerLayout = findViewById(R.id.drawer);
 drawerLayout.setScrimColor(Color.TRANSPARENT);
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
 this,
 drawerLayout,
 toolbar,
 R.string.app_name,
 R.string.app_name
 );
 toggle.syncState();
 drawerLayout.addDrawerListener(toggle);
 
 | 
3. 添加NavigationView
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | <RelativeLayoutandroid:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_gravity="left"
 >
 
 <com.google.android.material.navigation.NavigationView
 android:id="@+id/navigation_view"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 app:headerLayout="@layout/nav_header"
 app:menu="@menu/drawer_menu"
 
 />
 
 </RelativeLayout>
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 
 | <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#a2c699"
 android:orientation="vertical"
 android:padding="10dp">
 
 <ImageView
 android:id="@+id/image"
 android:layout_width="100dp"
 android:layout_height="100dp"
 android:layout_gravity="center"
 android:src="@drawable/ic_launcher_background"
 />
 
 <TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:layout_marginTop="8dp"
 android:text="小猫咪"
 android:textSize="22sp"
 android:textColor="#ffffff"
 />
 
 
 </LinearLayout>
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 
 | <?xml version="1.0" encoding="utf-8"?>
 <menu xmlns:android="http://schemas.android.com/apk/res/android">
 <group android:checkableBehavior="single">
 <item
 android:id="@+id/nav_favor"
 android:icon="@drawable/favor"
 android:title="我的收藏" />
 <item
 android:id="@+id/nav_balance"
 android:icon="@drawable/balance"
 android:title="我的钱包" />
 <item
 android:id="@+id/nav_contract"
 android:icon="@drawable/contact"
 android:title="朋友圈" />
 <item
 android:id="@+id/nav_friends"
 android:icon="@drawable/friend"
 android:title="联系人" />
 <item
 android:id="@+id/nav_message"
 android:icon="@drawable/message"
 android:title="我的消息" />
 <item
 android:id="@+id/nav_settings"
 android:icon="@drawable/setting"
 android:title="我的设置" />
 </group>
 </menu>
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 
 | NavigationView navigationView = findViewById(R.id.navigation_view);
 navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
 @Override
 public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
 switch (menuItem.getItemId()){
 case R.id.nav_favor:
 Toast.makeText(MainActivity.this, "我的收藏", Toast.LENGTH_SHORT).show();
 break;
 case R.id.nav_balance:
 Toast.makeText(MainActivity.this, "我的钱包", Toast.LENGTH_SHORT).show();
 break;
 case R.id.nav_contract:
 Toast.makeText(MainActivity.this, "我的朋友圈", Toast.LENGTH_SHORT).show();
 break;
 case R.id.nav_friends:
 Toast.makeText(MainActivity.this, "我的好友", Toast.LENGTH_SHORT).show();
 break;
 case R.id.nav_message:
 Toast.makeText(MainActivity.this, "我的消息", Toast.LENGTH_SHORT).show();
 break;
 case R.id.nav_settings:
 Toast.makeText(MainActivity.this, "我的设置", Toast.LENGTH_SHORT).show();
 break;
 }
 
 return true;
 }
 });
 
 | 
参考文章
Toolbar+DrawerLayout+NavigationView 实现抽屉效果,有关于设置偏移程度和全屏的内容
NavigationView基本使用,有关于设置menu选中和没有选中的样式