为什么 Toolbar 会被 TabLayout 覆盖?

Why Toolbar is overlaid with TabLayout?

我不知道为什么 header 向上滚动时 Toolbar 会被 Tablayout 覆盖。

但我只是希望它们不要像这张图片那样相互重叠

当 header 向上滚动并折叠时,我希望看起来像上一张图​​片。 我该如何解决? 请帮我。 先谢谢你了。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <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/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsingToolbarLayout"
                android:layout_width="match_parent"
                android:layout_height="@dimen/app_bar_height"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <ImageView
                    android:id="@+id/headerBg"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    android:contentDescription="@string/app_name"
                    app:layout_collapseMode="parallax"
                    app:layout_collapseParallaxMultiplier="0.7" />

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <Space
                        android:layout_width="40dp"
                        android:layout_height="40dp" />

                    <ImageView
                        android:id="@+id/profilePicture"
                        android:layout_width="@dimen/profile_pic_width"
                        android:layout_height="@dimen/profile_pic_height"
                        android:scaleType="centerCrop"
                        android:layout_gravity="center_horizontal"/>
                    <TextView
                        android:id="@+id/userName"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:textStyle="bold"
                        android:textSize="14dp" />
                </LinearLayout>

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:minHeight="?attr/actionBarSize"
                    android:gravity="top"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

                <android.support.design.widget.TabLayout
                    android:id="@+id/tabLayout"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_gravity="bottom"
                    app:layout_collapseMode="pin"
                    app:tabIndicatorColor="?attr/colorAccent"
                    app:tabIndicatorHeight="3dp"/>

            </android.support.design.widget.CollapsingToolbarLayout>

        </android.support.design.widget.AppBarLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#fff"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/navigation_drawer_items"
        app:itemIconTint="?attr/colorPrimary"
        app:itemTextColor="@color/black">

    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

尝试将 fitsSystemWindows=true 放入标签布局中,如下所示:

    <android.support.design.widget.TabLayout
                        android:id="@+id/tabLayout"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        android:layout_gravity="bottom"
                        app:layout_collapseMode="pin"
                        android:fitsSystemWindows="true"
                        app:tabIndicatorColor="?attr/colorAccent"
                        app:tabIndicatorHeight="3dp"/>

我不太确定,但可能是因为你在整个布局中使用了滚动条。只需添加一些 android:layout_marginTop,例如 60 dp。

将您的 TabLayout 移到 CollapsingToolbarLayout 之外,但仍在 AppBarLayout 之内。像这样

    <android.support.design.widget.AppBarLayout>
        <android.support.design.widget.CollapsingToolbarLayout>

            ...

        </android.support.design.widget.CollapsingToolbarLayout>

        <android.support.design.widget.TabLayout/>

    </android.support.design.widget.AppBarLayout>