Toolbar系列文章导航
Android中导航栏之Toolbar的使用
Android中导航栏之溢出菜单OverflowMenu
Android中导航栏之搜索框SearchView
Android中导航栏之自定义导航布局
Android中导航栏之标签导航暨TabLayout用法
之前我们说了Toolbar的各种用法,但此时我们就有小伙伴发出灵魂拷问了:太丑了,真的太丑了。哈?好吧,既然要求这么高,那么我们只能放大招了,开始自定义布局。其实很简单,就是在Toolbar的容器中加入我们自己的布局就可以了,示例代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><androidx.appcompat.widget.Toolbarapp:contentInsetStartWithNavigation="0dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/tl_head"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="中间"android:layout_centerInParent="true"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="左边"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:text="右边"/></RelativeLayout></androidx.appcompat.widget.Toolbar></LinearLayout>
这样我们就在Toolbar中定义了三个文字。我们还可以定义如日期选择器等功能,或者定制我们自己的标题。也可以定义右侧的图标按钮等。