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