首页 > 解决方案 > Android:使用 jetpack 导航,如何在不破坏后退按钮的情况下向片段添加折叠工具栏布局?

问题描述

我在下面有这个 collapsingtoolbar 代码,它工作得很好。但是,当我切换到使用 Jetpack Navigation(导航图)时,点击应用程序上的后退按钮会导致所有其他视图突然像这样向下移动。我怎样才能避免这种情况发生?

代码:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/diningAppBar"
        android:layout_width="match_parent"
        android:layout_height="356dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >

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

            <edu.msu.msumobile.ui.shared.CustomImageView
                android:id="@+id/diningImage"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                />

            <View
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:alpha="0.3"
                android:background="@android:color/black"
                android:fitsSystemWindows="true"/>

            <TextView
                android:id="@+id/diningHeader"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="?attr/actionBarSize"
                android:textColor="@android:color/white"
                android:textSize="48dp"
                android:paddingStart="@dimen/text_margin"
                android:paddingEnd="@dimen/text_margin"
                android:fitsSystemWindows="true"
                android:layout_gravity="bottom|end"
                android:gravity="bottom|end"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.5"
                />

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/diningDetailToolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                />

            <com.google.android.material.tabs.TabLayout
                android:id="@+id/diningDetailTabLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                app:tabIndicatorColor="@color/textLightColor"
                app:tabSelectedTextColor="@color/textSelectedColor"
                app:tabTextColor="@color/textLightColor"
                />

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

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

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/diningViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

白色区域是从包含上述代码的片段返回后添加的空间。它将在整个应用程序中持续存在,并且仅在具有此代码的片段上正确显示: 在此处输入图像描述

标签: androidandroid-collapsingtoolbarlayoutandroid-jetpackandroid-appbarlayoutandroid-jetpack-navigation

解决方案


解决了。原来,添加

android:fitsSystemWindows="true"

打破喷气背包导航。从上面的代码中删除那些删除了每个其他片段顶部多余的白条。


推荐阅读