The use of the Navigationview of Android design

Source: Internet
Author: User

The following explains the use of the tablayout under the design package, which will bring Navagationview and drawlayout as well as toolbar linkage.

Navigationview is easier to implement by providing the framework needed for drawer navigation, and it can directly generate navigation elements directly from menu resource files. Use Navigationview as a content view for drawerlayout. Navigationview handles the relationship with the status bar and ensures that the navigationview is properly interacting with the status bar on the api21+ device.

On the run diagram:

The following code is modified based on the preceding code.

Mainactivity.java

Package Com.example.nanchen.designtablayoutdemo;import Android.os.bundle;import Android.support.design.widget.navigationview;import Android.support.design.widget.tablayout;import Android.support.v4.view.gravitycompat;import Android.support.v4.view.viewpager;import Android.support.v4.widget.drawerlayout;import Android.support.v7.app.actionbardrawertoggle;import Android.support.v7.app.appcompatactivity;import Android.support.v7.widget.toolbar;import Android.view.MenuItem; Import Android.widget.toast;import Java.util.arraylist;import Java.util.list;public class Mainactivity extends    Appcompatactivity implements Navigationview.onnavigationitemselectedlistener {private drawerlayout DrawerLayout;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Viewpager VP = (viewpager) Findviewbyid (R.ID.MAIN_VP); Tablayout tablayout = (tablayout) Findviewbyid (r.id.main_tab_layout);       Toolbar Toolbar = (Toolbar) Findviewbyid (R.id.main_toolbar);        Drawerlayout = (drawerlayout) Findviewbyid (r.id.main_drawerlayout);        Set toolbar and drawerlayout to achieve animation and linkage This.setsupportactionbar (toolbar);        Actionbardrawertoggle Toggle = new Actionbardrawertoggle (this,drawerlayout,toolbar,0,0); Drawerlayout.adddrawerlistener (toggle);//Set monitoring toggle.syncstate ();//Plus synchronous Getsupportactionbar (). Setdefaultdisp        Layhomeasupenabled (TRUE);        list<string> list = new arraylist<> ();            for (int i = 1; i < 5; i++) {//List.add (String.Format (Locale.china, "02d% page", i));        List.add ("+i+" page ");        } vp.setadapter (New Myadapter (Getsupportfragmentmanager (), list));        Tablayout.setupwithviewpager (VP);        Navigationview Navigationview = (navigationview) Findviewbyid (r.id.main_navigation);    Navigationview.setnavigationitemselectedlistener (this); } @Override Public Boolean OnnavigationitemseleCTED (MenuItem Item) {switch (Item.getitemid ()) {Case R.id.menu_item1:toast.maketext (th                IS, "You clicked Menu 1", Toast.length_short). Show ();            Break                Case R.id.menu_item2:toast.maketext (This, "you clicked Menu 2", Toast.length_short). Show ();            Break                Case R.id.menu_item3:toast.maketext (This, "you clicked Menu 3", Toast.length_short). Show ();            Break                Case R.id.menu_item4:toast.maketext (This, "you clicked Menu 4", Toast.length_short). Show ();        Break        } drawerlayout.closedrawer (Gravitycompat.start);    return true; }}

Main layout

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "xmlns:app=" Http://schemas.android.com/apk/res-auto "Andro Id:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= "Com.example.nanchen.designtablayoutdemo.MainActivity" > <!--toolbar,actionbar Alternatives--< Android.support.v7.widget.Toolbar android:id= "@+id/main_toolbar" app:theme= "@style/themeoverlay.appcompat.da Rk. ActionBar "android:background=" @color/colorprimary "android:layout_width=" Match_parent "android:layou       t_height= "Actionbarsize"/> <android.support.v4.widget.drawerlayout android:layout_width= "Match_parent" android:layout_height= "0DP" android:id= "@+id/main_drawerlayout" android:layout_weight= "1" > < LinearLayout android:orientation= "verTical "android:layout_width=" match_parent "android:layout_height=" match_parent "> <andr Oid.support.design.widget.TabLayout android:layout_width= "match_parent" app:tabbackground= "@co Lor/colorprimarydark "app:tabtextcolor=" @android: Color/white "app:tabselectedtextcolor=" #04b4a E "android:layout_height=" Wrap_content "android:id=" @+id/main_tab_layout "/> <a Ndroid.support.v4.view.ViewPager android:layout_width= "match_parent" android:layout_height= "0d       P "android:layout_weight=" 1 "android:id=" @+id/main_vp "/> </LinearLayout> <android.support.design.widget.navigationview app:menu= "@menu/navigation" android:id= "@+id/main_nav Igation "android:layout_gravity=" Start "app:headerlayout=" @layout/header "app:itemicontint=" @color/navigation_selector "app:itemtextcolor=" @color/navigation_selector "android:layout_width=" Match_parent "Andro id:layout_height= "Match_parent" > </android.support.design.widget.NavigationView> </ Android.support.v4.widget.drawerlayout></linearlayout>

  

 

Navigation.xml

<?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/single_1" android:title= " Single choice 1 "android:icon=" @mipmap/ic_launcher "/>        <item android:id=" @+id/single_2 "android:title=" Radio 2 "Android: icon= "@mipmap/ic_launcher"/>        <item android:id= "@+id/single_3" android:title= "Radio 3" android:icon= "@mipmap /ic_launcher "/>    </group>    <item android:id=" @+id/menu_item1 "android:title=" Menu 1 "android:icon= "@mipmap/ic_launcher"/>    <item android:id= "@+id/menu_item2" android:title= "Menu 2" android:icon= "@mipmap/ic _launcher "/>    <item android:id=" @+id/menu_item3 "android:title=" Menu 3 "android:icon=" @mipmap/ic_launcher "/ >    <item android:id= "@+id/menu_item4" android:title= "Menu 4" android:icon= "@mipmap/ic_launcher"/></ Menu>

  

Header.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "              android:layout_width=" match_parent "              android:orientation=" vertical "              android:gravity=" Center "              android:background=" @drawable/header_bg "              android:layout_height=" 200DP ">    <imageview        android:layout_width= "wrap_content"        android:layout_height= "wrap_content"        android:src= "@mipmap/ic_ Launcher "/>    <textview        android:layout_width=" wrap_content "android:layout_height=" Wrap_        Content "        android:text=" text "/></linearlayout>

  

Header.bg.xml

<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" >    <gradient android:type= "linear" android:angle= "android:startcolor="              #143e52 android:endcolor= "#06a0ff"/></shape>

  

Navigation_selector.xml

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:color=" #fa08ca "android:state_checked=" true "/>    <item android:color=" # 6b6a6a "android:state_checked=" false "/></selector>

  

The use of the Navigationview of Android design

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.