Navigationview is a layout control in the new Android 5.0 feature--material design that can be used in conjunction with Drawerlayout to make the side-slip menu More aesthetically pleasing (you can add a head layout).
Navigationview needs to be nested inside drawerlayout, with the advantage of being able to add an additional Headerview head layout relative to the use of drawerlayout alone. In addition, the other options in Navigationview are written in the form of menu, and the branches in the menu can also form a column effect in the Navigationview.
As with other MD controls, using Navigationview requires registering dependencies in the Gradle file:
Compile ' com.android.support:design:24.1.1 '
1. Navigationview Properties:
App:headerLayout:NavigationView header layout, where you can store item items in a control app:menu:NavigationView such as ImageView, TextView , in the menu
2. Layout example:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Android:id= "@+id/activity_main"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "#FFFFFFFF"android:orientation= "vertical"> <Android.support.v7.widget.ToolbarAndroid:id= "@+id/toolbar"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "@color/colorprimary"Android:minheight= "? Attr/actionbarsize" /> <Android.support.v4.widget.DrawerLayoutAndroid:id= "@+id/drawer"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"> <!--Main interface Layout - <FramelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "#DDDDDD" /> <Android.support.design.widget.NavigationViewAndroid:id= "@+id/navigationview"Android:layout_width= "256.0dip"Android:layout_height= "Match_parent"android:layout_gravity= "Left"Android:background= "@color/colorprimary"android:fitssystemwindows= "true"App:headerlayout= "@layout/sideworks_navhead"App:menu= "@menu/navigate" /> </Android.support.v4.widget.DrawerLayout></LinearLayout>
The code in the menu file is as follows:
<Menuxmlns:android= "Http://schemas.android.com/apk/res/android"> <Group> <ItemAndroid:id= "@+id/one_one"Android:icon= "@mipmap/ic_launcher"Android:title= "Group one Item One" /> <ItemAndroid:id= "@+id/one_two"Android:icon= "@mipmap/ic_launcher"Android:title= "Group one Item" /> <ItemAndroid:id= "@+id/one_three"Android:icon= "@mipmap/ic_launcher"Android:title= "Group one Item three" /> </Group> <ItemAndroid:title= "Group"> <Menu> <ItemAndroid:id= "@+id/two_one"Android:icon= "@mipmap/ic_launcher"Android:title= "Group one Item One" /> <ItemAndroid:id= "@+id/two_two"Android:icon= "@mipmap/ic_launcher"Android:title= "Group one Item" /> </Menu> </Item></Menu>
The code in the head layout is as follows:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "#00000000"android:gravity= "Center_horizontal"android:orientation= "vertical"> <ImageViewAndroid:id= "@+id/header_image"Android:layout_width= "150.0dip"Android:layout_height= "150.0dip"Android:layout_margintop= "20.0dip"android:contentdescription= "@string/app_name"android:src= "@mipmap/ic_launcher" /> <TextViewAndroid:id= "@+id/header_title"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_marginbottom= "20.0dip"Android:layout_margintop= "10.0dip"Android:text= "Navigation View"Android:textcolor= "#FFFFFFFF"android:textsize= "18.0SP"Android:textstyle= "Bold" /></LinearLayout>
3. Pop-up drawer:
In the Java code, pop the drawer by clicking Actionbardrawertoggle in toolbar. The code is as follows:
// bind toolbar to activity Setsupportactionbar (toolbar); // set a button on the toolbar, click this button to pull out the drawer New Actionbardrawertoggle (mainactivity. This, drawer, toolbar, R.string.drawer_open, r.string.drawer_close); Toggle.syncstate (); Drawer.adddrawerlistener (toggle);
The final demo results are as follows:
The above is the simple use of Navigationview introduction, the following sticker code cloud on the source, for your reference.
Demo Address
Use of the Navigationview "ANDROID-MD"