首页 > 解决方案 > 折叠工具栏下方的 Android 内容高度

问题描述

我有一个带有导航抽屉和几个片段的应用程序。该应用程序有一个折叠工具栏,片段有各种内容。其中一些是来自 XML 的静态文件,另一些是在 recyclerview 中生成的。有些有FAB,有些没有。

我的问题是工具栏下方的任何布局,容器高度是根据折叠状态计算的。当我展开工具栏时,我希望下面的容器会缩小高度,但它只是被向下推,当我想在右下角放置一个 FAB 时,它就成了一个问题。这样,当我展开工具栏时,FAB 就会被推出屏幕。

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

                <View
                    android:id="@+id/collapse"
                    android:layout_width="wrap_content"
                    android:layout_height="256dp"
                    app:layout_collapseMode="parallax" />

                <com.google.android.material.appbar.MaterialToolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin" />

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

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

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:navGraph="@navigation/mobile_navigation" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer" />
</androidx.drawerlayout.widget.DrawerLayout>

所以我的问题是我应该做些什么来强制<fragment>元素填充应用栏下方的剩余位置并增加或缩小高度而不是部分滑出屏幕。

标签: androidandroid-constraintlayoutandroid-collapsingtoolbarlayout

解决方案


推荐阅读