首页 > 解决方案 > 滚动后如何让 CollapsingToolbarLayout 完全消失?

问题描述

我正在尝试用两个工具栏和它们之间的水平滚动视图制作一个片段“头部”。我想让水平滚动视图在向下滚动后完全消失,但在滚动后有一些黑色空间,我不知道为什么。我应该输入什么属性来实现它?

滚动前 滚动后

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:layout_height="match_parent"
    android:layout_width="match_parent">


    <androidx.appcompat.widget.Toolbar
        android:id="@+id/tool"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:foregroundGravity="center"
            android:src="@drawable/ic_arrow" />

    </androidx.appcompat.widget.Toolbar>


    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:clipToPadding="false"
        android:fitsSystemWindows="true"
        app:layout_constraintTop_toBottomOf="@id/tool"
        tools:context=".ui.menu.MenuFragment">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="220dp"
            android:fitsSystemWindows="true"
            android:theme="@style/Theme.PizzaTime.AppBarOverlay">


            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_collapseMode="parallax"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

                <HorizontalScrollView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fitsSystemWindows="true"
                    app:layout_collapseMode="parallax">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">
                    </LinearLayout>
                </HorizontalScrollView>

                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/support_action"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_gravity="bottom"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/Theme.PizzaTime.PopupOverlay">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"/>

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:foregroundGravity="center"
                        android:src="@drawable/ic_arrow" />

                </androidx.appcompat.widget.Toolbar>
            </com.google.android.material.appbar.CollapsingToolbarLayout>


        </com.google.android.material.appbar.AppBarLayout>

        <include layout="@layout/scroll_content" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidlayoutandroid-collapsingtoolbarlayoutandroid-appbarlayout

解决方案


在我从 CoordinatorLayout 中删除以下行之后:

android:fitsSystemWindows="true"

它开始正常工作,希望对某人有所帮助。


推荐阅读