Use of Android Material Design toolbar

Source: Internet
Author: User

Material design is Google's latest UI design specifications, I would like to actionbar everyone may not be unfamiliar, toolbar simply can be seen as an upgrade version of Actionbar, compared Actionbar, toolbar can be put anywhere, Seems more free, let's look at how to use toolbar:

In the build environment of Android Studio :

1: Need support of V7 package to import V7jar package in Build.gradle

dependencies {    Compile filetree (dir: ' Libs ', include: [' *.jar '])    compile ' com.android.support:appcompat-v7 : 23.1.1 '}

2. Block the action bar inside the theme in the style

<style name= "Appbasetheme" parent= "Theme.AppCompat.Light.NoActionBar" >          <!--toolbar (actionbar) Color-- >          <item name= "colorprimary" > #4876FF </item>          <!--status bar color--          <item name= " Colorprimarydark > #3A5FCD </item>          <!--window background color-          <item name= "Android:windowbackground" > @android:color/white</item>      </style>    

3. Define the toolbar in the XML layout, in general, we are a separate layout, and the direct include in the other required layouts is OK.

<android.support.v7.widget.toolbar xmlns:android= "Http://schemas.android.com/apk/res/android"      xmlns:app= "Http://schemas.android.com/apk/res/com.example.toolbar"      android:id= "@+id/toolbar"      android:layout_ Width= "Match_parent"      android:layout_height= "wrap_content"      android:background= "? Attr/colorprimary      " android:minheight= "? attr/actionbarsize"      app:popuptheme= "@style/themeoverlay.appcompat.light"      app:theme = "@style/themeoverlay.appcompat.actionbar" >     android:fitssystemwindows= "true" </ Android.support.v7.widget.toolbar>  

4. In the activity set with Setsupportactionbar settings, toolbar can replace the original Actionbar

Private Toolbar Mtoolbar;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Mtoolbar= (Toolbar) Findviewbyid (R.id.toolbar);        Setsupportactionbar (Mtoolbar);        Sets whether there is a return arrow       Getsupportactionbar (). Setdisplayhomeasupenabled (True);    

To this step basically can come out of effect such as: 2 is the use of an immersive status bar, the next article will mention

1 represents the return arrow, how to let the toolbar display the return arrow, there are two ways, in the corresponding actvity inside the definition (recommended these operations in baseactivity)

The first: define its parent activity directly inside the androidmanifest, and the child activity will show the return arrow

<activity            android:name= ". Secondactivity"            android:label= "@string/title_activity_second"            Android:parentactivityname= ". Mainactivity ">             <!--Parent activity meta-data to support 4.0 and lower          -- Currently supports more than 4.0, Meta-data is to let the lower version also support            <meta-data                android:name= "Android.support.PARENT_ACTIVITY"                Android:value= ". Mainactivity "/>        </activity>

The second type: code settings, sometimes we will have a layout multi-use situation, when his father activity is more than one, this time, we can not be set as the first one, we need to set in the code

In the parent class activity, set:
New Intent (this); Intent.putstring ("parent_activity_name", GetClass (). Getsimplename ()); getclass (). Getsimplename () Gets the current class name Tostaractivityf (successfulactivity. class, intent);
Carbon getsupportparentactivityintent () method in child activity
@Override PublicIntent getsupportparentactivityintent () {Intent parentintent=getintent (); String ClassName= Parentintent.getstringextra ("Parent_activity_name"); Intent newintent=NULL; Try { //You need to define the class with package nameNewintent =NewIntent (successfulactivity. This, Class.forName (Getpackagename () +className)); } Catch(ClassNotFoundException e) {e.printstacktrace (); } returnnewintent; }
I tested it myself, feeling that this method does not seem to be called, perhaps I am in baseactivity inside rewrite the following method, will he directly finish, we can study.
  @Override Public    Boolean onoptionsitemselected (MenuItem item) {        switch (Item.getitemid ()) {            // Override the behavior of the toolbar return button to prevent reopening the parent activity re-walk life cycle method case for            Android. R.id.home:                finish ();                return true;        }        return super.onoptionsitemselected (item);    }

Use of the toolbar for Android Material Design

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.