Android Studio Official version drawerlayout slide menu parsing

Source: Internet
Author: User

As follows

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/82/76/wKioL1dWPDSRFthkAAXbB2mdpXE316.gif "title=" Demo.gif "alt=" Wkiol1dwpdsrfthkaaxbb2mdpxe316.gif "/>

This is a new Drawerlayout project using Android studio to run out after the effect (no Android studio friends hurriedly go to download a bar).

After the generated code is a bit confusing, a little tidying up, then first look at the layout file

Activity_main.xml:

<?xml version= "1.0"  encoding= "Utf-8"? >  <android.support.v4.widget.drawerlayout  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:id=" @+id/drawer_layout "       android:layout_width= "Match_parent"       android:layout_ height= "Match_parent"       android:fitssystemwindows= "true"        tools:opendrawer= "Start" >        <include           layout= "@layout/app_bar_main"            android:layout_width= "Match_parent"            android:layout_height= "Match_parent"  />        < android.support.design.widget.navigationview           Android:id= "@+id/nav_view"           android:layout_width= " Wrap_content "          android:layout_height=" Match_parent "           android:layout_gravity= "Start"            android:fitssystemwindows= "true"            app:headerlayout= "@layout/nav_header_main"            app:menu= "@menu/activity_main_drawer"  />    </ Android.support.v4.widget.drawerlayout>

If App_bar_main.xml is referenced in Drawerlayout, Heraderlayout represents the green part of the slide-in menu in the Navigationview control, and menu represents the item.


App_bar_main.xml:

<?xml version= "1.0"  encoding= "Utf-8"?>  < Android.support.design.widget.coordinatorlayout 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"        android:fitssystemwindows= "true"       tools: context= "Com.lg.slidingmenudemo.MainActivity" >        < Android.support.design.widget.appbarlayout          android: Layout_width= "Match_parent"           android:layout_height= " Wrap_content "          andRoid:theme= "@style/apptheme.appbaroverlay" >             <android.support.v7.widget.Toolbar               android:id= "@+id/toolbar"                android:layout_width= "Match_parent"                android:layout_height= "Attr/actionbarsize"                android:background= "Attr/colorprimary"                app:popuptheme= "@style/ Apptheme.popupoverlay " />        </ Android.support.design.widget.appbarlayout>        <include  layout= "@layout/content_main"  />    </android.support.design.widget.coordinatorlayout> 

App_bar_main.xml in the toolbar can set the top title, feel reference content_main.xml troublesome words can be directly in the inside write layout, delete Content_main.xml can.


Mainactivity:

public class mainactivity extends appcompatactivity           implements NavigationView.OnNavigationItemSelectedListener {          @Override       protected void  OnCreate (bundle savedinstancestate)  {           Super.oncreate (savedinstancestate);           setcontentview ( R.layout.activity_main);          toolbar toolbar  =  (Toolbar)  findviewbyid (R.id.toolbar);           Setsupportactionbar (toolbar);             //Control side-slip menu          DrawerLayout drawer =  (Drawerlayout)  findviewbyid (R.id.drawer_layout);           actionbardrawertoggle toggle =  new actionbardrawertoggle (                   this, drawer, toolbar, r.string.navigation_drawer_open,  r.string.navigation_drawer_close);           Drawer.setdrawerlistener (toggle);           toggle.syncstate ( );             //setting selection events for Navigationview menu           NavigationView navigationView =  (Navigationview )  findviewbyid (R.id.nav_view);           Navigationview.setnavigationitemselectedlistener (This);      }           //back Key       @Override       public void  Onbackpressed ()  {          drawerlayout drawer  =  (drawerlayout)  findviewbyid (r.id.drawer_layout);           if  (Drawer.isdraweropen (gravitycompat.start))  {               drawer.closedrawer (Gravitycompat.start);           } else {               super.onbackpressed ();           }      }        @ Suppresswarnings ("Statementwithemptybody")        @Override        public boolean onnavIgationitemselected (Menuitem item)  {          int  id = item.getitemid ();             if  (Id == r.id.nav_camera)  {             } else if  (id == r.id.nav_gallery)  {             } else if  (id == r.id.nav_slideshow)  {             } else if  (id ==  r.id.nav_manage)  {            }  else if  (Id == r.id.nav_share)  {             } else if  (id == r.id.nav_send)  {              }            //close the slide-out menu           DrawerLayout drawer =  (drawerlayout )  findviewbyid (r.id.drawer_layout);           Drawer.closedrawer (Gravitycompat.start);           return  true;      }  }

You can also customize the menu

<?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_camera"               android:icon= "@drawable/ic _menu_camera "              android:title = "Import"  />          <item               android:id= "@+id/nav_gallery"                android:icon= "@drawable/ic_menu_ Gallery "&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;        android:title= "Gallery"  />           <item               android:id= "@+id/nav_slideshow"                android:icon= "@drawable/ic_menu_slideshow"                android:title= "Slideshow"  />           <item               android:id= "@+id/nav_manage"                android:icon= "@drawable/ic_menu_manage"                android:title= "Tools"  />      </ Group>  &nbsP;     <item android:title= "Communicate" >           <menu>               <item                   android:id= "@+id/nav_share"                    android:icon= "@drawable/ic_menu_share"                    android:title= "Share " />              <item                   android: Id= "@+id/nav_send"                    android:icon= "@drawable/ic_menu_send"                    android:title= "Send"  />           </menu>      </item>    </ Menu>

icon in item is the menu item icons Resource Id,title is the menu item title (the text that the menu item displays), which can be customized according to the requirements

Source Address: http://down.51cto.com/data/2221961

This article is from the "Android Development column" blog, be sure to keep this source http://liuyvhao.blog.51cto.com/11690759/1786857

Android Studio Official version drawerlayout slide menu parsing

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.