Android-drawerlayout Introduction

Source: Internet
Author: User

Drawerlayout has been out for a long time, personally feel that the domestic app are deeply poisoned by iOS in the design are scrambling to imitate the style of iOS, have forgotten what is the unique Android style, they have to learn the first and then with the product in the project to use a series of Android style, It happened that drawerlayout menu navigation was won by me and was used on the project.

First, drawerlayout exists in the V4 package, and we just need to configure it in XML

<android. Support. V4. Widgets. DrawerlayoutAndroid:id="@+id/drawerlayout"Xmlns:android="Http://schemas.android.com/apk/res/android"Android:layout_width="Match_parent"android:layout_height="Match_parent"> <textview android:layout_width="Match_parent"android:layout_height="Match_parent"android:text="@string/hello_world"Android:textcolor="@android: Color/black"android:gravity="Center"/> <linearlayout android:layout_width="Match_parent"android:layout_height="Match_parent"android:layout_gravity="Start"Android:background="#ffffff"android:orientation="Vertical"/></android. Support. V4. Widgets. Drawerlayout>

In this need to introduce the next Actionbardrawertoggle, control drawerlayout Display/hide use.
Use it in the two callback functions in the activity:
Onconfigurationchanged
onoptionsitemselected
Call Actionbardrawertoggle.syncstate () in the activity's onpostcreate (), indicating that the actionbardrawertoggle is synchronized with the Drawerlayout State, and set the drawer icon in the Actionbardrawertoggle to Actionbar's Home-button

 Public  class draweractivity extends appcompatactivity {Drawerlayout mdrawerlayout; Actionbardrawertoggle Toggle;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (r.layout.test_activity);        Mdrawerlayout = (drawerlayout) Findviewbyid (r.id.drawerlayout); Toggle =NewActionbardrawertoggle (draweractivity. This, Mdrawerlayout, R.string.hello_world, R.string.hello_world);        Mdrawerlayout.setdrawerlistener (toggle); Getsupportactionbar (). sethomebuttonenabled (true); Getsupportactionbar (). setdisplayhomeasupenabled (true); Getsupportactionbar (). Settitle ("Testdrawerlayout"); }@Override     Public void onpostcreate(Bundle savedinstancestate) {Super. Onpostcreate (Savedinstancestate);    Toggle.syncstate (); }@Override     Public void onconfigurationchanged(Configuration newconfig) {Super. onconfigurationchanged (Newconfig);    Toggle.onconfigurationchanged (Newconfig); }@Override     Public Boolean onoptionsitemselected(MenuItem Item) {if(toggle.onoptionsitemselected (item))return true;return Super. onoptionsitemselected (item); }}

The above steps set up to successfully use Drawerlayout in your project, but there are a few things to note:
1.android.support.v4.widget.drawerlayout is present under the V4 package
2. Themes need to show actionbar, such as Theme.AppCompat.Light.DarkActionBar
3.ActionBarDrawerToggle is a drawerlistener implementation, we can write our own drawerlistener to achieve the specific requirements.

We can find the unique Android style that we use is easy, but we are much affected by iOS.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android-drawerlayout Introduction

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.