Android ToolBar使用

ToolBar简介

ToolBar是Android 5.0推出的一个新的导航控件用于取代之前的ActionBar,由于其高度的可定制性、灵活性、具有Material Design风格等优点,越来越多的应用也用上了ToolBar,比如常用的知乎软件其顶部导航栏正是使用ToolBar。

 

ToolBar基本使用

更改主题

为了能够正常使用ToolBar,我们需要隐藏原来的ActionBar,这个可以在主题中修改,在res/values/styles.xml中做出如下修改:

 

 

在布局文件添加这个控件,代码如下:

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context="com.contentprovide.liuliu.toolbardemo.MainActivity">
 8 
 9     <android.support.v7.widget.Toolbar
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content">
12 
13         <TextView
14             android:layout_width="wrap_content"
15             android:layout_height="wrap_content"
16             android:text="标题栏" />
17 
18     </android.support.v7.widget.Toolbar>
19 
20 </android.support.constraint.ConstraintLayout>

 

 

 

可以看到我在ToolBar里面放了一个TextView控件,这个ToolBar就相当于一个ViewGroup。在它内部放的view就可以自定义标题栏样式了.

运行程序,效果:

 

 

要进一步完善标题栏只需要在里面添加控件,也可以加上点击事件让功能更完善

 

posted @ 2018-01-04 16:11  西红柿里没有番茄  阅读(167)  评论(0编辑  收藏  举报