微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

根据其状态更改折叠工具栏的插图

如何解决根据其状态更改折叠工具栏的插图

我有折叠工具栏和一些布局,我想折叠它。为了防止视图进入状态栏,我使用系统插图来设置折叠工具栏的边距。我将 AppBarLayout 提取到单独的文件并将其包含在 CoordinatorLayout 中:

<com.google.android.material.appbar.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/action_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/app_white"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:background="@color/colorToolbar"
            android:elevation="4dp"
            android:focusable="true"
            android:focusableInTouchMode="true"
            app:contentInsetStartWithNavigation="0dp"
            app:layout_collapseMode="pin">

            ...
        </androidx.appcompat.widget.Toolbar>

        <com.google.android.material.card.MaterialCardView
            android:id="@+id/bonuses_widget"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardCornerRadius="14dp"
            app:cardElevation="3dp"
            app:layout_collapseMode="parallax">

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

MaterialCardView 的边距是通过代码设置的,因为布局不支持加法。我使用插图为折叠工具栏添加边距:

ViewCompat.setonApplyWindowInsetsListener(action_bar) { _,insets ->
    collapsing_toolbar.setMarginTop(insets.systemWindowInsetTop)
    insets
}

这在折叠工具栏展开时效果很好,但在折叠状态下工具栏位于状态栏下方:

我已阅读 this 问题,已实现 AppBarStatechangelistener 并像这样使用它:

action_bar.addOnOffsetChangedListener(object : AppBarStatechangelistener() {
    override fun onStateChanged(appBarLayout: AppBarLayout,state: State) {
        if (state == State.COLLAPSED) {
            ViewCompat.setonApplyWindowInsetsListener(collapsing_toolbar) { _,insets ->
                toolbar.setMarginTop(insets.systemWindowInsetTop)
                insets
            }
        }
        if (state == State.EXPANDED) {
            ViewCompat.setonApplyWindowInsetsListener(collapsing_toolbar) { _,insets ->
                toolbar.setMarginTop(0)
                insets
            }
        }
    }
})

这没有帮助,据我目前所知,状态栏的插图已经由操作栏处理,折叠工具栏没有任何处理。
我还尝试在折叠工具栏的同时向工具栏留边距,它有效但导致了另一个问题:

毕竟我尝试删除插图并将 android:fitsSystemWindows="true" 设置为操作栏,这解决了状态栏下工具栏的问题,但状态栏意外地获得了奇怪的颜色(紫色),这在应用程序颜色中没有表现出来。
希望有人知道在这种情况下可以正确处理插图。

解决方法

您可以尝试在您的 setSupportActionBar() 中调用 Fragment,因为这对我有用:

(requireActivity() as AppCompatActivity).apply {
    //supportActionBar?.hide()    //may need this to hide the Activity actionBar

    setSupportActionBar(binding.toolbar)
}

setHasOptionsMenu(true)    //don't forget this

演示:https://youtu.be/kmnsep_V-jE

,

最终我找到了合适的解决方案:

ViewCompat.setOnApplyWindowInsetsListener(action_bar) { view,insets ->
    view.updatePadding(top = insets.systemWindowInsetTop)
    insets
}

状态栏下的白色背景仍然存在问题(我需要它像左侧屏幕截图一样变暗),但最初的问题已解决。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。