Introduction to the use of NavigationView

Navigation View (NavigationView) is usually used in combination with DrawerLayout (DrawerLayout) to achieve a good sliding interaction experience. From the perspective of conventional development, side sliding is generally implemented on the left side, and the drawer layout can have three sub-layouts. (Note: The first sub-layout must be the main interface, and the second two sub-layouts are the layouts on the left and right sides, and only one on the left and right sides can be placed)

First add reference in build.gradle

implementation 'com.android.support:design:26.1.0'

Below is the interface xml file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
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"
tools:openDrawer="start">
    <include
layout=                            
        "@layout/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
    <android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_drawer_layout_one"
/>
</android.support.v4.widget.DrawerLayout>                
                                                        

Among them, include is the main interface, NavigationView is the sliding menu, and header is the header file. Of course, you can also add menu (conventional sliding menu).

Cancel the scrollbars of the side sliding menu:

/**
 * Hide the bar
 of the side sliding menu */
 private void disableNavigationViewScrollbars (NavigationView navigationView) {
     if (navigationView != null ) {
        NavigationMenuView navigationMenuView = (NavigationMenuView) navigationView.getChildAt(0);
        if (navigationMenuView != null) {
            navigationMenuView.setVerticalScrollBarEnabled( false ) ;
        
    }
}

Of course, there are also the opening and closing events of the side slide menu:

drawer.openDrawer(Gravity.LEFT);
drawer.closeDrawer(Gravity.LEFT);

Get the head control:

View headView = navigationView.getHeaderView(0);
mMenuIcon = headView.findViewById(R.id.menu_icon);
At the end, what I want to say is that I have never wanted to use menu and fill in all the layout with the header file, but the screen is not completely filled on the simulator. The real machine test has not found any problems for the time being. The embarrassing thing is that I query the highest version of the api documentation. It is 19. The source code is not queried. Do you have any method? I beg






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324677713&siteId=291194637