Android uses Drawerlayout to quickly implement a slide-out menu

Source: Internet
Author: User

I. Overview

Drawerlayout is an easy to implement Android side-by-side menu component, I recently developed a project also has a side-by-side menu function, so drawerlayout comes in handy. If you have never used drawerlayout, then this blog will use a simple case to take you quickly to master the use of drawerlayout.

Two

Third, the code implementation

Main layout Activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  xmlns:app  =" Http://schemas.android.com/apk/res-auto " Span class= "Hljs-attribute" >android:layout_width  = "match_parent"  android:layout_height  = "match_parent"  android:background  =" @android: Color/white " android:fitssystemwindows  =" true " android:orientation  = "vertical" ;     <android.support.v7.widget.toolbar  android:id  =  Android:layout_width  = "match_parent"  android : layout_height  = "wrap_content"  android: Background  = "@color/bluestatus"  android: MinHeight  = "attr/actionbarsize"  app: Navigationicon  =  App:theme  = "@style/theme.appcompat.noactionbar" ;     </android.support.v7.widget.Toolbar>    <include layout="@layout/title_layout" />    <android.support.v4.widget.DrawerLayout xmlns:tools="http://schemas.android.com/ Tools "android:id="@+id/drawer_layout "android:layout_width=" Match_ Parent "android:layout_height="match_parent "tools:opendrawer=" Start ">                                        <ListViewandroid:id="@+id/listview"android:layout_width="Match_ Parent "android:layout_height=" match_parent " />                                            <linearlayoutandroid:layout_width="260DP"android:layout_height ="Match_parent"android:layout_gravity="Right">                                                <include layout="@layout/drawer_content" />        </linearlayout>    </android.support.v4.widget.DrawerLayout></linearlayout>

To use a drawerlayout, position your primary content view as the first child with a width and height of match_parent. ADD drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.

When you use Drawerlayout, the first element of Drawerlayout is the main content area (the ListView in this case), and its height must be match_parent.
Add a skid view after the main content area (in this case, Drawer_content.xml), and set layout_gravity to determine whether it is left or right, usually the height of this slide-by view is set to Match_parent.

Drawer_content.xml

<?xml version="1.0"encoding="Utf-8"?> <linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"Android:layout_width="Match_parent"android:layout_height="Match_parent"Android:background="@color/background"android:orientation="Vertical"android:padding="@dimen/activity_horizontal_margin"> <textview style="@style/normaltextview"Android:layout_width="Wrap_content"android:layout_height="40DP"android:text="Chinese name of ship"/> <edittext style="@style/smallgreytextview"Android:layout_width="Match_parent"android:layout_height="40DP"Android:background="@drawable/btn_round_white"android:padding="@dimen/margin_8"/> <textview style="@style/normaltextview"Android:layout_width="Wrap_content"android:layout_height="40DP"android:text="Ship English name"/> <edittext style="@style/smallgreytextview"Android:layout_width="Match_parent"android:layout_height="40DP"Android:background="@drawable/btn_round_white"android:padding="@dimen/margin_8"/> <textview style="@style/normaltextview"Android:layout_width="Wrap_content"android:layout_height="40DP"android:text=" call sign"/> <edittext style="@style/smallgreytextview"Android:layout_width="Match_parent"android:layout_height="40DP"Android:background="@drawable/btn_round_white"android:padding="@dimen/margin_8"/> <textview style="@style/normaltextview"Android:layout_width="Wrap_content"android:layout_height="40DP"android:text="IMO"/> <edittext style="@style/smallgreytextview"Android:layout_width="Match_parent"android:layout_height="40DP"Android:background="@drawable/btn_round_white"android:padding="@dimen/margin_8"/> <textview style="@style/normaltextview"Android:layout_width="Wrap_content"android:layout_height="40DP"android:text="MMSI"/> <edittext style="@style/smallgreytextview"Android:layout_width="Match_parent"android:layout_height="40DP"Android:background="@drawable/btn_round_white"android:padding="@dimen/margin_8"/> <relativelayout android:layout_width="Match_parent"android:layout_height="0DP"android:layout_weight="1"> <button android:id="@+id/btn_confirm"style="@style/normalgreytextview"Android:layout_width="80DP"android:layout_height="36DP"android:layout_alignparentright="true"Android:layout_centerinparent="true"android:layout_gravity="Center_vertical"Android:background="@drawable/btn_round_red"android:gravity="Center"android:text="Query"Android:textcolor="@color/white"/> </relativelayout> </Linearlayout>

This layout file is the slide-by view,

MainActivity.java
 PackageCom.leohan.drawerlayoutdemo;ImportAndroid.os.Bundle;ImportAndroid.support.v4.view.GravityCompat;ImportAndroid.support.v4.widget.DrawerLayout;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.support.v7.widget.Toolbar;Importandroid.view.Gravity;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView;ImportAndroid.widget.LinearLayout;ImportAndroid.widget.ListView;ImportAndroid.widget.TextView;ImportJava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.List;ImportJava.util.Map;ImportButterknife. Butterknife;ImportButterknife. Injectview;ImportButterknife. OnClick; Public  class mainactivity extends appcompatactivity {    @InjectView(r.id.toolbar) toolbar toolbar;@InjectView(R.id.tv_search) TextView Tvsearch;@InjectView(R.id.listview) ListView listview;@InjectView(r.id.drawer_layout) Drawerlayout drawerlayout;PrivateList data =NewArrayList ();PrivateShiprecordadapter adapter =NewShiprecordadapter ( This, data);@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); Butterknife.inject ( This);        Setsupportactionbar (toolbar);        Listview.setadapter (adapter);    GetData (); }@Override     Public void onbackpressed() {if(Drawerlayout.isdraweropen (Gravitycompat.start))        {Drawerlayout.closedrawer (Gravitycompat.start); }Else{Super. onbackpressed (); }    }/** * Get category Data * /    Private void GetData() { for(inti =0; I <6; i++) {map<string, object> Map =NewHashmap<> ();        Data.add (map);    } adapter.notifydatasetchanged (); }@OnClick(R.id.tv_search) Public void OnClick(View view) {Switch(View.getid ()) { CaseR.id.tv_search:if(Drawerlayout.isdraweropen (Gravity.right))                {Drawerlayout.closedrawer (gravity.right); }Else{Drawerlayout.opendrawer (gravity.right); } Break; }    }}

Since the slide-in view here is sliding from the right side, the layout_gravity of the slide-in view is set to either "Yes" or "end", or left or start if the slide is on. When we manually control the opening or closing of the slide view, we need to execute the following code:

//closedrawerLayout.closeDrawer(Gravity.RIGHT);//opendrawerLayout.openDrawer(Gravity.RIGHT);

At this point the basic use of drawerlayout has mastered, more in-depth understanding of drawerlayout, please refer to Google Official document creating a Navigation Drawer.

Project Source: Https://github.com/leoleohan/DrawerLayoutDemo

Android uses Drawerlayout to quickly implement a slide-out menu

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.