The existing app tries to align to material design and begins using toolbar instead of the previous actionbar.
the visual difference between toolbar and Actionbar is that need our own to be ToolBar add to your own layout file. The current situation is: changes in our existing projects, most of the activity is to inherit a baseactivity. In order to achieve the goal with minimal cost, after simple thinking, the following changes have been made:
/span>
/** * Base class with Toolbar */public class Baseactivity extends Actionbaractivity {private static final int base_view_id = R.lay Out.activity_base; private static final Layoutparams Layout_params = new Layoutparams (layoutparams.match_parent, Layoutparams.match_ PARENT); Private LinearLayout Mparentview; Private Toolbar Mtoolbar; @Override protected void OnCreate (Bundle savedinstancestate) {viewutils.starttranslucent (this); Super.oncreate (savedinstancestate); Setcontentview (base_view_id); } @Override public void Setcontentview (int layoutresid) {if (base_view_id = = Layoutresid) {Super . Setcontentview (LAYOUTRESID); Mparentview = (linearlayout) Findviewbyid (R.id.base_parent_view); Mtoolbar = (Toolbar) Findviewbyid (R.id.toolbar); Inittoolbar (Mtoolbar); Return } mparentview.addview (Getlayoutinflater (). Inflate (Layoutresid, null), layout_params); } @Override public voidSetcontentview (view view) {Mparentview.addview (view, layout_params); } private void Inittoolbar (Toolbar Toolbar) {Setsupportactionbar (Toolbar); Getsupportactionbar (). Setdisplayhomeasupenabled (True); Toolbar.setnavigationonclicklistener (New View.onclicklistener () {@Override public void OnClick (View V) {finish (); } }); } public Toolbar Gettoolbar () {return mtoolbar; } public void SetBackground (int colorid) {if (null! = Mparentview) {Mparentview.setbackgroundcolor ( Getresources (). GetColor (Colorid)); } }}
Layout file Activity_base.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android " android:id=" @+id/base_parent_view " android:layout_width=" match_parent " android:layout _height= "Match_parent" android:background= "@color/global_background_status_bar" android: Fitssystemwindows= "true" android:orientation= "vertical" > <include layout= "@layout/subview_toolbar "/></linearlayout>
In the base class to add the above code, the basic can be used with minimal changes to achieve the purpose of using toolbar. However, there is also a problem, that is, we have a layer of each activity's layout level.
If there is a better idea, welcome to share ~ ~
A simple package for Android Toobar