Android Note: Use of drawerlayout drawer layouts

Source: Internet
Author: User

Drawerlayout (drawer layout), often appearing in various apps, such as CSDN.

Points:

1. When using Drawerlayout, in the XML layout, put the layout of the main interface in front, then put the layout contents of the drawer behind

2. Remember to add android:layout_gravity= "left" or "start" to the layout in the drawer to indicate which side the drawer appears on.

The code looks like this:

Activity_drawer.xml

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" >    <Android.support.v4.widget.DrawerLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/drawer_layout"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" >        <!--The layout of the main interface must be placed in front of the drawer -        <FramelayoutAndroid:id= "@+id/content_frame"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" />        <!--contents of Drawer -        <ListViewAndroid:id= "@+id/left_drawer"Android:layout_width= "240DP"Android:layout_height= "Match_parent"android:layout_gravity= "Left"Android:background= "#ffffff"Android:choicemode= "Singlechoice"Android:divider= "@android: Color/transparent"Android:dividerheight= "1DP" />    </Android.support.v4.widget.DrawerLayout></Relativelayout>

Values/strings.xml

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <stringname= "App_name">Drawerlayouttest</string>    <stringname= "Action_settings">Settings</string>    <stringname= "Drawer_close">Close drawer</string>    <stringname= "Drawer_open">Open the drawer.</string>        <String-arrayname= "Planets_array">        <Item>Home</Item>        <Item>Found</Item>        <Item>Collection</Item>        <Item>Set up</Item>         <Item>Draft</Item>        <Item>History</Item>        <Item>Mode</Item>    </String-array>    </Resources>

Drawerlayoutdemo.java

 Packagecom.example.drawerlayouttest;ImportAndroid.os.Bundle;Importandroid.app.Activity;Importandroid.content.res.Configuration;ImportAndroid.graphics.Color;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.AdapterView.OnItemClickListener;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView;ImportAndroid.support.v4.app.ActionBarDrawerToggle;ImportAndroid.support.v4.view.GravityCompat;Importandroid.support.v4.widget.DrawerLayout; Public classDrawerlayoutdemoextendsactivity{Privatestring[] Mplanettitles; PrivateDrawerlayout mdrawerlayout; PrivateActionbardrawertoggle Mdrawertoggle; PrivateListView mdrawerlist; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_drawer); Mdrawerlayout=(drawerlayout) Findviewbyid (r.id.drawer_layout); //Initialize ListViewInitlistview (); //Add drawer SwitchMdrawertoggle =NewActionbardrawertoggle ( This, Mdrawerlayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {            //Drawer closed             Public voidondrawerclosed (view view) {Invalidateoptionsmenu ();//creates call to Onprepareoptionsmenu ()            }           //Drawer Open             Public voidondraweropened (View drawerview) {invalidateoptionsmenu ();        }        };               Mdrawerlayout.setdrawerlistener (Mdrawertoggle); //enable ActionBar app icon to behave as action to toggle nav drawerGetactionbar (). setdisplayhomeasupenabled (true); //Getactionbar (). Sethomebuttonenabled (true);    }    Private voidInitlistview () {mdrawerlist=(ListView) Findviewbyid (r.id.left_drawer); Mplanettitles=getresources (). Getstringarray (R.array.planets_array); Mdrawerlist.setadapter (NewArrayadapter<string> ( This, Android.                R.layout.simple_list_item_1, Mplanettitles)); //Setting up Listener eventsMdrawerlist.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?>Parent, view view,intPositionLongID) {mdrawerlist.setitemchecked (position,true);                Settitle (Mplanettitles[position]);            Mdrawerlayout.closedrawer (mdrawerlist);    }        }); }       //callback after activity is run@Overrideprotected voidonpostcreate (Bundle savedinstancestate) {Super. Onpostcreate (savedinstancestate); //Sync the toggle state after onrestoreinstancestate have occurred.mdrawertoggle.syncstate (); }    //no need to restart activity when configuration changes@Override Public voidonconfigurationchanged (Configuration newconfig) {Super. onconfigurationchanged (Newconfig);    Mdrawertoggle.onconfigurationchanged (Newconfig); }    //Set Options Menu@Override Public Booleanonoptionsitemselected (MenuItem item) {//Pass the event to Actionbardrawertoggle, if it returns//true, then it has handled the app icon touch event        if(mdrawertoggle.onoptionsitemselected (item)) {return true; }        return Super. onoptionsitemselected (item); }}

Icon for drawer switch:

After the code runs, open the drawer, the effect

Android Note: Use of drawerlayout drawer layouts

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.