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.

Package Com.example.nanchen.designtablayoutdemo;import Android.os.bundle;import;import;import;import;import;import;import;import;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 (;       Toolbar Toolbar = (Toolbar) Findviewbyid (;        Drawerlayout = (drawerlayout) Findviewbyid (;        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 (;    Navigationview.setnavigationitemselectedlistener (this); } @Override Public Boolean OnnavigationitemseleCTED (MenuItem Item) {switch (Item.getitemid ()) {Case (th                IS, "You clicked Menu 1", Toast.length_short). Show ();            Break                Case (This, "you clicked Menu 2", Toast.length_short). Show ();            Break                Case (This, "you clicked Menu 3", Toast.length_short). Show ();            Break                Case (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= " Android "xmlns:tools=" Http:// "xmlns:app=" Http:// "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: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: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 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 android:layout_width= "match_parent" android:layout_height= "0d       P "android:layout_weight=" 1 "android:id=" @+id/main_vp "/> </LinearLayout> < 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" > </> </></linearlayout>




<?xml version= "1.0" encoding= "Utf-8"? ><menu xmlns:android= "Http://" >    <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>



<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= " 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>

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



<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= " 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: 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.