如何让 CoordinatorLayout 调整其依赖视图的大小而不是移动它

问题描述

我正在尝试重新设计我的应用程序的 AppBars 以使用 CollapsingToolbarLayout 模式。随着内容的滚动,工具栏的大小会减小,而可见的内容区域会增加

我的应用广泛使用 Fragments。 Activity 布局只包含 AppBar,而 Fragment 视图在运行时插入。

CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout 的标准示例代码适用于我的大部分 UI,但某些屏幕的项目位于可滚动区域的底部。无论滚动状态如何,这些都是固定的并且始终可见。

我遇到的问题是,当工具栏完全展开时,CoordinatorLayout 通过垂直向下偏移整个相关视图来实现其折叠工具栏魔术。这意味着底部元素不可见。它们在滚动条折叠时变得可见,但我需要它们始终可见。

我认为,我真正需要的是一种不同的行为,其中从属视图不会随着工具栏高度的变化而偏移,而是会调整大小以使其始终完全可见。实现这一目标的最佳方法是什么?

这是当前行为(左侧的 Android Studio 布局检查器,因此您可以看到系统导航栏隐藏的内容框架)。当工具栏展开时,我需要红色文本视图留在屏幕上:

Bottom of view hidden when scrolled

这是演示布局代码

<?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="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/Theme.ProminentBarTest.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_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

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

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

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <!-- Content of this FrameLayout is really a Fragment layout and inserted
        at runtime. Child Views can't be moved out into the activity layout -->

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

        <androidx.core.widget.nestedScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text="@string/large_text" />

        </androidx.core.widget.nestedScrollView>

        <TextView
            android:id="@+id/problem_view"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:text="Bottom item"
            android:layout_gravity="bottom"
            android:gravity="center"
            android:background="@color/design_default_color_error"
            android:textColor="@color/white" />

        </LinearLayout>
    </FrameLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

我意识到这个简单演示的解决方案是将 @id/problem_view 从其父 FrameLayout 中移出,成为 CoordinatorLayout 的直接子项,但实际应用程序在 Activity 中使用包含的 Fragment,因此视图需要与其他 Fragment 视图的布局相同。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)