Android's super easy way to implement the native Sideslip menu

Source: Internet
Author: User
Tags integer xmlns

Let's take a look at the effect chart.

When you click on the menu can change the icon, such as Click Happy, Home will change a smiley face, the implementation of the process is super simple

You need to use the toolbar and drawablelayout two to compare the new controls

First write three XML layout files, I have the layout file here is embedded with the include tag, the code is as follows

Headbar_toolbar.xml

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16 <?xml version= "1.0" encoding= "Utf-8"?> <android.support.v7.widget.toolbar xmlns:android= "http://" Schemas.android.com/apk/res/android "android:id=" @+id/tbheadbar "android:layout_width=" Match_parent "Android: layout_height= "50DP" android:background= "@color/red" > <textview android:layout_width= "wrap_content" Android: layout_height= "wrap_content" android:layout_gravity= "center" android:text= "@string/emotion" android:textcolor= "@ Color/white "android:textsize=" 16sp "/> </android.support.v7.widget.Toolbar>

My_drawablelayout.xml

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The

Main_activity.xml

?

1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 <?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns: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:orientation= "vertical" tools:context= "Com.demo.usher.demo_slidingmenu". Mainactivity "> <!--head--> <include layout=" @layout/headbar_toolbar "/> <!--main layout--> <include out= "@layout/my_drawablelayout"/> </LinearLayout>

How to apply the "one file" in the Java file

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 Package com.demo.usher.demo_slidingmenu; &nbsp; Import Android.os.Bundle; Import Android.support.v4.widget.DrawerLayout; Import Android.support.v7.app.ActionBarDrawerToggle; Import android.support.v7.app.AppCompatActivity; Import Android.support.v7.widget.Toolbar; Import Android.view.View; Import Android.widget.AdapterView; Import Android.widget.ArrayAdapter; Import Android.widget.ImageView; Import Android.widget.LinearLayout; Import Android.widget.ListView; &nbsp; Import java.util.ArrayList; Import java.util.List; &nbsp; Import Butterknife. BindView; Import Butterknife. Butterknife; &nbsp; public class Mainactivity extends Appcompatactivity {&nbsp; @BindView (r.id.tbheadbar) Toolbar Mtbheadbar; &nbsp;/ * Sideslip Menu Layout * * * @BindView (R.id.llmenu) LinearLayout Mllmenu; &nbsp;/* sideslip menu ListView Place menu item */@BindView (r.id.lvmenu) ListView Mlvmenu; &nbsp; @BindView (r.id.ivcontent) ImageView mivcontent; &nbsp; @BindView (R.id.dlmenu) drawerlayout mmydrawable; &nbsp; Actionbardrawertoggle Mtoggle; &nbsp; Private list&lt;string&gt; lvmenulist = new Arraylist&lt;string&gt; () {Add ("angry"); Add ("Happy"); Add ("sad"); Embarrassed "); }}; &nbsp; Private list&lt;integer&gt; imageList = new Arraylist&lt;integer&gt; () {Add (r.drawable.angry); Add ( R.drawable.happy); Add (R.drawable.sad); Add (r.drawable.embarrassed); }}; &nbsp; @Override protected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Butterknife.bind (this); &nbsp;/* Initialization of toolbar and drawablelayout*/Inittoolbaranddrawablelayout (); &nbsp; Mlvmenu.setadapter New Arrayadapter (this, Android. R.layout.simple_expandable_list_item_1, lvmenulist)); Mlvmenu.setonitemclicklistener (New Adapterview.onitemclicklistener () {@Override public void Onitemclick (Adapterview &lt;?&gt; Parent, view view, int position, long id) {Mivcontent.setimageresource (Imagelist.get (position)); Mmydrawable.closedrawers ()/* Close drawer/}}); } &nbsp; private void Inittoolbaranddrawablelayout () {SetsupportactionbaR (Mtbheadbar); /* The following two methods to set the return key can be * * Getsupportactionbar (). Sethomebuttonenabled (True); Getsupportactionbar (). Setdisplayhomeasupenabled (True); /* Set caption text cannot be displayed/Getsupportactionbar (). setdisplayshowtitleenabled (false); &nbsp; Mtoggle = new Actionbardrawertoggle (this, mmydrawable, Mtbheadbar, R.string.open, r.string.close) {@Override Publ IC void ondraweropened (View drawerview) {super.ondraweropened (Drawerview);//toast.maketext (Mainactivity.this, R.string.open, Toast.length_short). Show (); } &nbsp; @Override public void ondrawerclosed (View drawerview) {super.ondrawerclosed (Drawerview);//toast.maketext ( Mainactivity.this, R.string.close, Toast.length_short). Show (); } }; /*mmydrawable.setdrawerlistener (mtoggle); not recommended * * Mmydrawable.adddrawerlistener (Mtoggle); Mtoggle.syncstate ();/* Sync status/}}

About Butterknife Annotations and styles

Butterknife directly in the Gradle file is configured as follows "the lack of what to complement what"

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The

Style is modified in the style file about the color style of the return key

?

1 2 3 4 5 6 7 8 9 10 11 <resources> <style name= "Apptheme" parent= "Theme.AppCompat.Light.NoActionBar" > <item name= " Drawerarrowstyle "> @style/apptheme.drawerarrowtoggle</item> </style> <style name=" Apptheme.drawerarrowtoggle "parent=" Base.Widget.AppCompat.DrawerArrowToggle "> <item name=" Color ">@ Android:color/white</item> </style> </resources>

Summarize

In fact, many times when we use a Third-party control often do not know how to implement the behind, the use of native controls can let us better understand an interaction or to implement a function of the principle, is conducive to the performance and interaction are very good app, the above is the entire content of this article, Hope to the work of everyone or learn to bring certain help.

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.