写一个menu文件作为抽屉内容
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="hehe" android:icon="@mipmap/ic_launcher"></item>
<item android:title="hehe" android:icon="@mipmap/ic_launcher"></item>
<item android:title="hehe" android:icon="@mipmap/ic_launcher"></item>
<item android:title="hehe" android:icon="@mipmap/ic_launcher"></item>
<item android:title="hehe" android:icon="@mipmap/ic_launcher"></item>
</menu>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.drawerlayout.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer">
//先写父布局
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_btn"
android:text="开关抽屉"/>
//后写抽屉布局
<com.google.android.material.navigation.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:headerLayout="@layout/layout_head"
app:menu="@menu/menu_navigation"
android:layout_gravity="left"
>
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
</LinearLayout>
@Override
public void onClick(View v) {
int id = v.getId();
if(layout.isDrawerOpen(Gravity.LEFT)){
layout.closeDrawer(Gravity.LEFT);
}else{
layout.openDrawer(Gravity.LEFT);
}
}