Android AppBarLayout和折叠ToolBarLayout-收缩内容

Android AppBarLayout和折叠ToolBarLayout-收缩内容,android,android-layout,android-collapsingtoolbarlayout,android-appbarlayout,Android,Android Layout,Android Collapsingtoolbarlayout,Android Appbarlayout,我有AppBarLayout和CollasingToolbarLayout。在内部,我有一个ImageView作为背景,并用TextView约束Yout。滚动时的预期行为:AppBarLayout缩小到刚好可以看到整个ConstraintLayout。不幸的是,对我来说,ConstraintLayout向上移动,并且消失在视野之外 以下是我希望它看起来的样子: 在滚动之前 滚动后 相反,这是我现在拥有的: 在滚动之前 滚动后 我查看了CollasingToolbarLayout教程,

我有AppBarLayout和CollasingToolbarLayout。在内部,我有一个ImageView作为背景,并用TextView约束Yout。滚动时的预期行为:AppBarLayout缩小到刚好可以看到整个ConstraintLayout。不幸的是,对我来说,ConstraintLayout向上移动,并且消失在视野之外

以下是我希望它看起来的样子:

在滚动之前

滚动后

相反,这是我现在拥有的:

在滚动之前

滚动后



我查看了CollasingToolbarLayout教程,但找不到它只是缩小而不是实际折叠成工具栏的任何内容。

您在代码中编写的所有内容都无法正常工作。如果您想获得预期屏幕截图中显示的结果,您需要为包含项目的每个视图/视图组编写
CoordinatorLayout.Behavior
。并将其设置为您的视图/视图组

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ScrollingActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="262dp"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.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="#00FFFFFF"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
                android:src="@drawable/img_dashboard"/>

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="140dp"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@+id/content"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="18dp"
                    android:layout_marginBottom="18dp"
                    android:layout_gravity="bottom|center_horizontal">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:layout_marginBottom="4dp"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintBottom_toTopOf="@id/featured_main_text"
                        app:layout_constraintTop_toTopOf="parent">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Offline"
                            android:textColor="#FFFFFF"
                            android:textSize="12sp"
                            android:layout_marginStart="28dp"
                            android:layout_marginEnd="12dp"/>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="2 hrs ago"
                            android:textColor="#FFFFFF"
                            android:textSize="12sp" />

                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/featured_main_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintBottom_toBottomOf="parent">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="John Smith"
                            android:textColor="#FFFFFF"
                            android:textSize="18sp"
                            android:layout_marginStart="28dp" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Place Holder"
                            android:textColor="#FFFFFF"
                            android:textSize="18sp"
                            android:layout_marginStart="28dp" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Place Holder"
                            android:textColor="#FFFFFF"
                            android:textSize="18sp"
                            android:layout_marginStart="28dp" />

                    </LinearLayout>

                </androidx.constraintlayout.widget.ConstraintLayout>

            </androidx.appcompat.widget.Toolbar>


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

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

</androidx.coordinatorlayout.widget.CoordinatorLayout>