目录
效果图
布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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"
tools:context=".MainActivity">
<!-- 内容区 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="内容区"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_open_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开左边"/>
<Button
android:id="@+id/btn_open_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开右边"/>
</LinearLayout>
<!-- 左边菜单 -->
<LinearLayout
android:id="@+id/menu_view"
android:layout_width="260dp"
android:orientation="vertical"
android:background="@color/white"
android:gravity="center"
android:layout_height="match_parent"
android:layout_gravity="start">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="左边菜单"
android:textColor="@color/myorange"
android:textSize="20sp"
android:textStyle="bold"/>
<Button
android:id="@+id/btn_close_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭"/>
</LinearLayout>
<!-- 右边菜单 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="@color/white"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="右边菜单"
android:textColor="@color/myorange"
android:textSize="20sp"
android:textStyle="bold"/>
<Button
android:id="@+id/btn_close_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭"/>
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
Activity
case R.id.btn_open_left:
//打开左边
drawerLayout.openDrawer(Gravity.LEFT);
break;
case R.id.btn_open_right:
//打开右边
drawerLayout.openDrawer(Gravity.RIGHT);
break;
case R.id.btn_close_left:
//关闭左边
drawerLayout.closeDrawer(Gravity.LEFT);
break;
case R.id.btn_close_right:
//关闭所有
drawerLayout.closeDrawers();
break;