ToolBar不做过多的介绍,直接写如何使用。
1.在XML布局文件中加入ToolBar控件,用V7包里面的控件。否则只支持5.0以上的(在其它文章中看到的,没有去验证)。
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_doing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/colorPrimary">
</android.support.v7.widget.Toolbar>
2.(1)如果没有隐藏掉标题栏(ActionBar),就需要使用一个方法替换掉ActionBar,加入ToolBar。
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_doing);
setSupportActionBar(toolbar);
这样就能达到想要的效果。
注意:
如果使用了setSupportActionBar()方法,会遇到ToolBar的方法使用了但是没有效果的现象。比如使用toolbar.inflateMenu(R.menu.titlebar_menu);
这是因为加载的是onCreateOptionsMenu()中的布局。在该方法中添加布局文件即可。还有设置toolbar的title。需要在setSupportActionBar()方法前使用。
(2)如果隐藏了标题栏,在 setContentView();方法前调用supportRequestWindowFeature(Window.FEATURE_NO_TITLE);(AppCompatActivity调用这个方法),或者使用的是没有ActionBar的主题,在AndroidManifest.xml中查看。
这种情况下直接绑定
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_doing);
然后使用toolbar的各种方法即可。
学习ToolBar,参考文章:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1118/2006.html