0

布局如下图

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EEEEEE">
 
    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:backgroundTint="#4E342E"
        app:contentInsetStart="0dp"
        app:fabAlignmentMode="end"
        app:fabCradleMargin="8dp"
        app:fabCradleRoundedCornerRadius="32dp"
        app:hideOnScroll="true">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom">

            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottomNavigationView"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="#4E342E"
                app:elevation="0dp"
                app:itemIconTint="#F48FB1"
                app:itemTextColor="#F48FB1"
                app:labelVisibilityMode="unlabeled"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintWidth_default="percent"
                app:layout_constraintWidth_percent=".72"
                app:menu="@menu/main" />
        </androidx.constraintlayout.widget.ConstraintLayout>

    </com.google.android.material.bottomappbar.BottomAppBar>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:backgroundTint="#D81B60"
        app:layout_anchor="@id/bottomAppbar"
        app:srcCompat="@drawable/ic_favorite"
        app:tint="@color/white" />

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="256dp"
        android:background="#f00"
        android:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ff0"
            app:contentScrim="@android:color/holo_purple"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/ivHeader"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@drawable/header"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7" />

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways"
                app:menu="@menu/main"
                app:title="Hello World" />

        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

并在视图层次结构中的每个项目中调用消耗插图

        appbar?.setOnApplyWindowInsetsListener { view, insets ->
            insets.consumeSystemWindowInsets()
            view.updatePadding(top = insets.systemWindowInsetTop)

            println(" Appbar padding: ${view.paddingTop}, inset top: ${insets.systemWindowInsetTop}, " +
                    "isConsumed: ${insets.isConsumed}")

            insets
        }
        collapsingToolbar?.setOnApplyWindowInsetsListener { view, insets ->

            view.updatePadding(top =  insets.systemWindowInsetTop)
            insets.consumeSystemWindowInsets()
            println(" collapsingToolbar padding: ${view.paddingTop}, inset top: ${insets.systemWindowInsetTop}, " +
                "isConsumed: ${insets.isConsumed}")
            insets
        }

        ivHeader?.setOnApplyWindowInsetsListener { view, insets ->

            //            view.updatePadding(top =  insets.systemWindowInsetTop)
            println(" ivHeader padding: ${view.paddingTop}, inset top: ${insets.systemWindowInsetTop}, "+
                    "isConsumed: ${insets.isConsumed}")
            insets
        }

        bottomAppbar?.setOnApplyWindowInsetsListener { view, insets ->
            println(" bottomAppbar padding: ${view.paddingTop}, inset bottom: ${insets.systemWindowInsetTop}, " +
                    "isConsumed: ${insets.isConsumed}")
            insets.consumeSystemWindowInsets()
//            view.updatePadding(bottom = insets.systemWindowInsetBottom)
            insets
        }

        // Add padding to bottom of BottomNavigationView to display it above NavigationBar
        val bottomNav = findViewById<BottomNavigationView>(R.id.bottomNavigationView)

        bottomNav.setOnApplyWindowInsetsListener { view, insets ->
            view.updatePadding(bottom = 0)
            insets.consumeSystemWindowInsets()
            println(" BottomNav padding bottom: ${view.paddingTop}, inset bottom: ${insets.systemWindowInsetTop}, " +
                    "isConsumed: ${insets.isConsumed}")
            insets
        }

哪个打印

I:  bottomAppbar padding: 0, inset bottom: 83, isConsumed: false
I:  BottomNav padding bottom: 0, inset bottom: 83, isConsumed: false
I:  Appbar padding: 83, inset top: 83, isConsumed: false
I:  collapsingToolbar padding: 83, inset top: 83, isConsumed: false
I:  ivHeader padding: 0, inset top: 83, isConsumed: false
I:  bottomAppbar padding: 0, inset bottom: 83, isConsumed: false
I:  BottomNav padding bottom: 0, inset bottom: 83, isConsumed: false
I:  Appbar padding: 83, inset top: 83, isConsumed: false
I:  collapsingToolbar padding: 83, inset top: 83, isConsumed: false
I:  ivHeader padding: 0, inset top: 83, isConsumed: false

为什么setOnApplyWindowInsetsListener即使我调用消耗,插图也不消耗任何内容。

编辑

在日志中添加了消耗的插图,但它们都没有显示除 0 以外的任何值。

  bottomAppbar padding: 0, inset bottom: 83isConsumed: false
I: consumedInsets: WindowInsets{systemWindowInsets=Insets{left=0, top=0, right=0, bottom=0} stableInsets=Insets{left=0, top=0, right=0, bottom=0} sysGestureInsets=Insets{left=0, top=0, right=0, bottom=0}}
I:  BottomNav padding bottom: 0, inset bottom: 83isConsumed: false
I: consumedInsets: WindowInsets{systemWindowInsets=Insets{left=0, top=0, right=0, bottom=0} stableInsets=Insets{left=0, top=0, right=0, bottom=0} sysGestureInsets=Insets{left=0, top=0, right=0, bottom=0}}
I: ☕️ Fab padding bottom: 0, inset bottom: 83
I: consumedInsets: WindowInsets{systemWindowInsets=Insets{left=0, top=0, right=0, bottom=0} stableInsets=Insets{left=0, top=0, right=0, bottom=0} sysGestureInsets=Insets{left=0, top=0, right=0, bottom=0}}
I:  Appbar padding: 83, inset top: 83isConsumed: false
I: consumedInsets: WindowInsets{systemWindowInsets=Insets{left=0, top=0, right=0, bottom=0} stableInsets=Insets{left=0, top=0, right=0, bottom=0} sysGestureInsets=Insets{left=0, top=0, right=0, bottom=0}}
I:  collapsingToolbar padding: 83, inset top: 83isConsumed: false
I: consumedInsets: WindowInsets{systemWindowInsets=Insets{left=0, top=0, right=0, bottom=0} stableInsets=Insets{left=0, top=0, right=0, bottom=0} sysGestureInsets=Insets{left=0, top=0, right=0, bottom=0}}
I:  ivHeader padding: 0, inset top: 83isConsumed: false
I: consumedInsets: WindowInsets{systemWindowInsets=Insets{left=0, top=0, right=0, bottom=0} stableInsets=Insets{left=0, top=0, right=0, bottom=0} sysGestureInsets=Insets{left=0, top=0, right=0, bottom=0}}
I:  bottomAppbar padding: 0, inset bottom: 83isConsumed: false
I: consumedInsets: WindowInsets{systemWindowInsets=Insets{left=0, top=0, right=0, bottom=0} stableInsets=Insets{left=0, top=0, right=0, bottom=0} sysGestureInsets=Insets{left=0, top=0, right=0, bottom=0}}
I:  BottomNav padding bottom: 0, inset bottom: 83isConsumed: false
I: consumedInsets: WindowInsets{systemWindowInsets=Insets{left=0, top=0, right=0, bottom=0} stableInsets=Insets{left=0, top=0, right=0, bottom=0} sysGestureInsets=Insets{left=0, top=0, right=0, bottom=0}}
I: ☕️ Fab padding bottom: 42, inset bottom: 83
I: consumedInsets: WindowInsets{systemWindowInsets=Insets{left=0, top=0, right=0, bottom=0} stableInsets=Insets{left=0, top=0, right=0, bottom=0} sysGestureInsets=Insets{left=0, top=0, right=0, bottom=0}}
I:  Appbar padding: 83, inset top: 83isConsumed: false
I: consumedInsets: WindowInsets{systemWindowInsets=Insets{left=0, top=0, right=0, bottom=0} stableInsets=Insets{left=0, top=0, right=0, bottom=0} sysGestureInsets=Insets{left=0, top=0, right=0, bottom=0}}
I:  collapsingToolbar padding: 83, inset top: 83isConsumed: false
I: consumedInsets: WindowInsets{systemWindowInsets=Insets{left=0, top=0, right=0, bottom=0} stableInsets=Insets{left=0, top=0, right=0, bottom=0} sysGestureInsets=Insets{left=0, top=0, right=0, bottom=0}}
I:  ivHeader padding: 0, inset top: 83isConsumed: false
I: consumedInsets: WindowInsets{systemWindowInsets=Insets{left=0, top=0, right=0, bottom=0} stableInsets=Insets{left=0, top=0, right=0, bottom=0} sysGestureInsets=Insets{left=0, top=0, right=0, bottom=0}}

并且有这个输出在此处输入图像描述

如何修复 BottomAppBar 和 AppBarLayout 的插图?

4

0 回答 0