【问题标题】:AppBarLayout and CollapsingToolbarLayout - shrink contentAppBarLayout 和 CollapsingToolbarLayout - 缩小内容
【发布时间】:2020-10-22 15:39:04
【问题描述】:

我有带有 CollapsingToolbarLayout 的 AppBarLayout。在里面我有一个 ImageView 作为背景,以及带有 TextViews 的 ConstraintLayout。滚动时的预期行为:AppBarLayout 缩小到足以使整个 ConstraintLayout 仍然可见。不幸的是,发生在我身上的是 ConstraintLayout 向上移动并且不在视线范围内。

这是我想要的样子:

滚动前

滚动后

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

滚动前

滚动后

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

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

【问题讨论】:

    标签: android android-layout android-collapsingtoolbarlayout android-appbarlayout


    【解决方案1】:

    您在代码中编写的所有内容都不会开箱即用。如果您想获得它在您预期的屏幕截图中显示的结果,您需要为每个包含您的项目的 View/viewGroup 编写CoordinatorLayout.Behavior。并将其设置为您的 View/ViewGroup。

    【讨论】:

      猜你喜欢
      • 2016-04-23
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-10
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多