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; 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; Import java.util.ArrayList; Import java.util.List; Import Butterknife. BindView; Import Butterknife. Butterknife; public class Mainactivity extends Appcompatactivity { @BindView (r.id.tbheadbar) Toolbar Mtbheadbar; / * Sideslip Menu Layout * * * @BindView (R.id.llmenu) LinearLayout Mllmenu; /* sideslip menu ListView Place menu item */@BindView (r.id.lvmenu) ListView Mlvmenu; @BindView (r.id.ivcontent) ImageView mivcontent; @BindView (R.id.dlmenu) drawerlayout mmydrawable; Actionbardrawertoggle Mtoggle; Private list<string> lvmenulist = new Arraylist<string> () {Add ("angry"); Add ("Happy"); Add ("sad"); Embarrassed "); }}; Private list<integer> imageList = new Arraylist<integer> () {Add (r.drawable.angry); Add ( R.drawable.happy); Add (R.drawable.sad); Add (r.drawable.embarrassed); }}; @Override protected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Butterknife.bind (this); /* Initialization of toolbar and drawablelayout*/Inittoolbaranddrawablelayout (); Mlvmenu.setadapter New Arrayadapter (this, Android. R.layout.simple_expandable_list_item_1, lvmenulist)); Mlvmenu.setonitemclicklistener (New Adapterview.onitemclicklistener () {@Override public void Onitemclick (Adapterview <?> Parent, view view, int position, long id) {Mivcontent.setimageresource (Imagelist.get (position)); Mmydrawable.closedrawers ()/* Close drawer/}}); } 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); 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 (); } @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.