具体的布局,根据情况使用include导入该布局
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/main_content_frame_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="100dp"
android:onClick="openRightLayout"
android:text="右边" />
</RelativeLayout>
<!-- 右侧滑动栏 开启抽屉后显示的布局
宽度高度都可以根据实际情况设置宽高
-->
<RelativeLayout
android:id="@+id/main_right_drawer_layout"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="@color/colorPrimary"
android:paddingTop="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="右边菜单测试" />
</RelativeLayout>
</androidx.drawerlayout.widget.DrawerLayout>
java代码,
//初始化控件
public void initLayout() {
drawerLayout = (DrawerLayout) findViewById(R.id.main_drawer_layout);
//右边菜单
main_right_drawer_layout = (RelativeLayout) findViewById(R.id.main_right_drawer_layout);
}
//打开右边的抽屉
public void openRightLayout(View view) {
if (drawerLayout.isDrawerOpen(main_right_drawer_layout)) {
drawerLayout.closeDrawer(main_right_drawer_layout);
} else {
drawerLayout.openDrawer(main_right_drawer_layout);
}
}
以上是一个dome,在华为10上可以完美跑起来