【问题标题】:Custom layout in Toolbar with TabLayout below工具栏中的自定义布局,下面有 TabLayout
【发布时间】:2017-08-28 23:25:33
【问题描述】:

现在,我有一个正常的Toolbar。我要做的是在ToolbarTabLayout之间添加一个自定义布局,如下图所示:

左边是我的Toolbar 现在的样子,右边是我想要的样子。

如您所见,我想在布局中添加一个ImageView 和两个TextView

我怎样才能做到这一点?

这是我目前的布局:

<?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>

【问题讨论】:

  • 您希望该部分折叠吗?还是只显示您显示的展开高度?
  • @ianhanniballake 最好,当用户向下滚动时,我希望 ToolbarTabLayout 都消失,并且当用户向上滚动时(但不是一直到顶部),只显示Toolbar 而不是TabLayout,当用户一直滚动到顶部时,同时显示整个ToolbarTabLayout。如果这不是可能的,那么我希望它们都在滚动时消失,并且仅在用户一直滚动到顶部时显示。

标签: android android-layout android-studio android-toolbar android-appbarlayout


【解决方案1】:

AppBarLayout 扩展了LinearLayout,因此您可以将任意数量的视图添加到您的AppBarLayout:没有理由专门将视图添加到您的Toolbar

如果您希望它们滚动相同,则需要确保使用与 Toolbar 相同的 layout_scrollFlags

【讨论】:

  • 这似乎可行,但我发现了一些问题。当我开始在RecyclerView 区域中滚动时,RecyclerView 将在AppBarLayout 完全滚动到视野之外之前开始滚动(即RecyclerViewAppBarLayout 同步滚动)。 Here's an image of what's happening。但是,如果我在屏幕的AppBarLayout 部分滚动,它会按应有的方式滚动。有时,如果我在屏幕的RecyclerView 部分轻轻一晃,它可能会在AppBarLayout 滚动出视图之前穿过几个项目。
  • 其次,是否可以设置遵循这种模式的滚动值?当用户向下滚动时,我希望 ToolbarTabLayout 都消失,并且当用户向上滚动(但不是一直到顶部)时,只显示 Toolbar(不是配置文件布局位,只是Toolbar 的最顶部)而不是TabLayout,最后当用户一直滚动到顶部时,显示整个ToolbarTabLayout
  • AppBarLayout 控制所有的滚动。我还没有看到您看到的第一个滚动问题,但是投掷肯定是一个已知的更棘手的问题。您可以尝试添加 android:minHeight="0dp" 并使用 app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" 将您的个人资料部分折叠起来,直到您滚动到顶部。
  • 你有什么方法可以建议处理这个问题吗?另外,我应该在什么视图上应用android:minHeight="0dp"app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollap‌​sed"?我尝试将其应用于Toolbar,但这似乎不起作用。
【解决方案2】:

Toolbar 只是一个 ViewGroup,您可以随意自定义。

试试这个:

<android.support.v7.widget.Toolbar
 style="@style/ToolBarStyle"
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="?attr/colorPrimary"
 android:minHeight="@dimen/abc_action_bar_default_height_material">

<ImageView
    android:layout_width="wrap_content"
    android:contentDescription="@string/logo"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:src="@drawable/ic_launcher"/>

这应该将你的 imageView 放在工具栏的中心。

【讨论】:

    猜你喜欢
    • 2015-10-18
    • 2022-01-17
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多