Android NavigationView 侧滑菜单

1.在res-menu下新建nav_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:icon="@drawable/abc_vector_test"
            android:title="Call"/>
        <item
            android:icon="@drawable/abc_vector_test"
            android:title="Call"/>
        <item
            android:icon="@drawable/abc_vector_test"
            android:title="Call"/>
        <item
            android:icon="@drawable/abc_vector_test"
            android:title="Call"/>
        <item
            android:icon="@drawable/abc_vector_test"
            android:title="Call"/>
    </group>

    <group android:id="@+id/other">
        <item
            android:id="@+id/sign_out"
            android:title="退出登录"/>
    </group>
</menu>

2.header布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#3872A7"
    android:layout_width="match_parent"
    android:layout_height="300dp">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/icon_image"
        android:src="@color/colorPrimaryDark"
        android:layout_centerInParent="true"
        android:layout_width="70dp"
        android:layout_height="70dp" />

    <TextView
        android:layout_alignParentBottom="true"
        android:text="账号:100001"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>

3.activity_layout

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/draw">

    <Button
        android:id="@+id/btn"
        android:text="btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigationView"
        app:menu="@menu/nav_menu"
        app:headerLayout="@layout/nav_header"
        android:layout_gravity="left"
        android:background="#9B9B9B"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>

</androidx.drawerlayout.widget.DrawerLayout>

4.菜单的点击事件

 navigationView = findViewById(R.id.navigationView);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
     
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
     
                //在这里处理item的点击事件
                switch (item.getItemId()) {
     
                    case R.id.sign_out:
                        Log.e("-----------", "sign_out");
                        break;
                }
                return true;
            }
        });

5.显示

   draw.openDrawer(GravityCompat.START);

6.header控件的点击事件
去掉NavigationView里的app:headerLayout="@layout/nav_header"

 private NavigationView nav_sliding_menu_view;
    private View nav_header_view;
    private TextView nav_sliding_menu_account_textview;
  nav_sliding_menu_view = (NavigationView) view.findViewById(R.id.nav_sliding_menu_view);
        nav_header_view = nav_sliding_menu_view.inflateHeaderView(R.layout.nav_sliding_menu_header);
        nav_sliding_menu_account_textview = nav_header_view.findViewById(R.id.nav_sliding_menu_account_textview);
  nav_sliding_menu_account_textview.setOnClickListener(new View.OnClickListener() {
     
            @Override
            public void onClick(View view) {
     
                L.e("nav_sliding_menu_account_textview");
            }
        });

你可能感兴趣的:(Android,Android)