Android Design Support Library uses Navigation View

Android Design Support Library brings some important Material Design components to developers and is downward compatible with Android 2.1. Navigation View is one of them, which can be used to easily create navigation drawers.


The implementation steps are as follows:

Add the following code to the layout file:

 <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!-- What needs to be presented -->
        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"

            app:menu="@menu/drawer"/>
  </android.support.v4.widget.DrawerLayout>
app:menu is used to specify menu items for navigation drawers. The code is as follows:

<?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_home"
            android:icon="@drawable/ic_dashboard"
            android:title="Home" />
        <item
            android:id="@+id/nav_messages"
            android:icon="@drawable/ic_event"
            android:title="Messages" />
        <item
            android:id="@+id/nav_friends"
            android:icon="@drawable/ic_headset"
            android:title="Friends" />
        <item
            android:id="@+id/nav_discussion"
            android:icon="@drawable/ic_forum"
            android:title="Discussion" />
    </group>
    <item android:title="Sub items">
        <menu>
            <item
                android:icon="@drawable/ic_dashboard"
                android:title="Sub item 1" />
            <item
                android:icon="@drawable/ic_forum"
                android:title="Sub item 2" />
            <item
                android:icon="@drawable/ic_forum"
                android:title="Sub item 2" />
        </menu>
    </item>
</menu>
app:headerLayout is used to specify an arbitrary layout as the top of the navigation drawer, such as the purple band Username font in the rendering.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:background="@color/titleBlue"
    >
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/userpic"
    android:layout_centerInParent="true"
    android:id="@+id/Iamge_mine_head"
    />
</RelativeLayout>

java partial code

  mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
                   NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
                   headerLayout = navigationView.inflateHeaderView(R.layout.drawer_header);
                   head = (ImageView) headerLayout.findViewById(R.id.Iamge_mine_head);
                   head.setOnClickListener(new View.OnClickListener() {
                       @Override
                       public void onClick(View view) {
                           Toast.makeText(MainActivity.this,"Member login",Toast.LENGTH_LONG).show();
                       }
                   });

                   if (navigationView != null) {
                       navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                           @Override
                           public boolean onNavigationItemSelected(MenuItem menuItem) {
                               //Switching corresponding Fragment s and other operations
                               menuItem.setChecked(true);
                               mDrawerLayout.closeDrawers();
                               if(menuItem.toString().equals("Home")){
                                   Toast.makeText(MainActivity.this,menuItem.toString(),Toast.LENGTH_LONG).show();
                               }
                               return false;
                           }
                       });
                   }

Tags: Android DrawerLayout xml encoding

Posted on Tue, 12 Feb 2019 18:36:18 -0500 by patrickrock