ToolbarBriefly
Toolbar in the v7.21+ package, is a replacement for actionbar components, can be said to be Actionbar upgrade version. There are several features of toolbar compared with Actionbar:
- Actionbar belongs to the decorative component of window, one activity can only have one actionbar, but toolbar belongs to the view level, can have any number of
- Actionbar customization difficult, toolbar can be regarded as a viewgroup, can be freely paired with
Several compositions of toolbar are similar to those of Actionbar, and the order is:
- Navigation buttons
- Logo
- Title
- Custom components
- Action menu (Similar menu)
UseReplace Actionbar
If you are using a Compatibility pack, you still have to inherit actionbaractivity, the only point: you need to remove Actionbar from the theme.
<style name= "V7. Toolbar "parent=" Theme.AppCompat.Light.DarkActionBar "> <item name=" Windowactionbar ">false</item ></style> or <style name= "V7. Toolbar "parent=" Theme.AppCompat.Light.NoActionBar "></style>
Replace Actionbar with toolbar in your code
Setcontentview (R.layout.v7_activity_toolbar); Toolbar Toolbar = (Toolbar) Findviewbyid (r.id.v7_toolbar_1); if (Toolbar! = null) { Setsupportactionbar (toolbar); }
It's so simple.
Implement tab
It can be noted that the Setnavigationmode method in Actionbar has been marked as deprecated, so it must not be recommended.
Toolbar does not provide a ready-made implementation, although it brings inconvenience, but gives greater freedom.
Just add the tab component to the Custom Components section of toolbar, the tab component is too much, and Google also provides an implementation slidingtablayout.
<android.support.v7.widget.toolbar android:id= "@+id/v7_toolbar_2" android:layout_width= "Match_ Parent " android:layout_height=" 60DP > < Dev.xesam.android.study.lollipop.v7.toolbar.SlidingTabLayout android:id= "@+id/v7_sliding_tabs" Android : layout_width= "match_parent" android:layout_height= "Wrap_content"/></ Android.support.v7.widget.toolbar>
Implementing NAV List
With tab, replace Slidingtablayout with spinner (older models may need to look for additional components to assist with the desired effect).
<android.support.v7.widget.toolbar android:id= "@+id/v7_toolbar_1" android:layout_width= "Match_ Parent " android:layout_height=" 60DP " app:theme=" @style/v7. Toolbartheme "> <spinner android:id=" @+id/v7_toolbar_spinner " android:layout_width=" Wrap_ Content " android:layout_height=" Wrap_content "/></android.support.v7.widget.toolbar>
Demo
Demo (dev.xesam.android.study.lollipop.v7.toolbar.ToolbarActivity)
Android Share Q Group: 315658668
"Android" Toolbar