A simple package for Android Toobar

Source: Internet
Author: User

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

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.