工具栏中的自定义布局,下面是 TabLayout
现在,我有一个普通的工具栏
.我想要做的是在Toolbar
和TabLayout
之间添加一个自定义布局,如下图所示:
Right now, I have a normal looking Toolbar
. What I want to do is add a custom layout between the Toolbar
and the TabLayout
, as shown in the picture below:
左边是我的 Toolbar
现在的样子,右边是我想要的样子.
On the left is what my Toolbar
looks like now, and on the right is what I want it to look like.
如您所见,我想在布局中添加一个 ImageView
和两个 TextView
.
As you can see, I want to add an ImageView
and two TextView
s to the layout.
我怎样才能做到这一点?
How can I achieve this?
这是我当前的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="48dp"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
AppBarLayout 扩展了 LinearLayout
,因此您可以向 AppBarLayout
添加任意数量的视图:没有理由将视图专门添加到您的 工具栏
.
AppBarLayout extends LinearLayout
, so you can add any number of views to your AppBarLayout
: there's no reason to add the views specifically to your Toolbar
.
如果您希望它们滚动相同,请确保使用与 Toolbar
相同的 layout_scrollFlags
.
You'll want to make sure you use the same layout_scrollFlags
as your Toolbar
if you want them to scroll the same.