Toolbar 是在 Android 5.0推出的一个非常方便使用的一个导航控件。今天我们来学习使用一下。首先我们查看一下TooBar的源码。
可以知道TooBar继承自ViewGroup我们可以往里面添加我们自己的布局
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
toolbar:navigationIcon="@mipmap/ic_drawer_home"
toolbar:theme="@style/AppThemeMyToobar"
toolbar:logo="@mipmap/ic_launcher"
toolbar:titleTextColor="@color/white"
toolbar:title="标题"
toolbar:subtitleTextColor="@color/white"
toolbar:subtitle="啦啦啦"
>
<!--自定义控件-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello world"/>
</android.support.v7.widget.Toolbar>
运行情况如图所示
这里有个坑我们要注意。Toolbar属性设置无效
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_0176da"
android:logo="@mipmap/ic_launcher"
android:navigationIcon="@mipmap/ic_drawer_home"
android:subtitle="456"
android:title="123">
<!--自定义控件-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello world" />
</android.support.v7.widget.Toolbar>
</LinearLayout>
设置的 logo、navigationIcon、subtitle、title 都没有!只要把没有用的设置全部改成自己定义的命名空间。