The toolbar of android5.0

Source: Internet
Author: User

Toolbar is android5.0 after the proposed, you can simply understand the Actionbar upgrade, after 5.0, the official recommended to use the toolbar! under the text of the toolbar (old, there are questions please correct, there are questions, we discuss together!) Rattle...... )

①toolbar is 5.0 after the proposed, then the lower version of what to do ah? The usual, introduced V7 package ' com.android.support:appcompat-v7:22.1.1 ', is a referral! (Note: Some of the following classes are in the V7 package, do not write wrong!!!) )

After the ②V7 package is introduced, it is still not possible to use the toolbar directly, but under the Styles.xml file, specify the following relationship:

<name= "Apptheme"  parent= "Theme.AppCompat.Light.NoActionBar"  >

In other words, it is toolbar and actionbar can not coexist, the use of toolbar must be removed from the Actionbar, so it must have the above designation.

③ can now use toolbar in the layout file, but for compatibility, it must be in the V7 package . Specify the following:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Android.support.v7.widget.Toolbarxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:id= "@+id/toolbar"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Wrap_content"6 Android:background= "#ff88ff43"7 android:orientation= "vertical">8 9 </Android.support.v7.widget.Toolbar>

④ in Mainactivity.java, find the toolbar corresponding ID, and then set the toolbar (V7 package), it should be noted that at this time mainactivity inherit must be V7 package , the key code is as follows:

1  Private void Initview () {2        Setcontentview (r.layout.activity_main); 3         toolbar = (toolbar) Findviewbyid (R.id.toolbar); 4         // Set A to act as the-this Activity window. 5         Setsupportactionbar (toolbar); 6     }

toolbar, it's done! It's simple! Just pay attention to those few points and there is no problem at all!

Code Download: Https://github.com/SamSarah1/Android-Demo

The toolbar of android5.0

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.