如何避免在沉浸模式下调整视图大小?

How to avoid views resizing in immersive mode?

当我点击“播放”时我正在使用沉浸模式,当我按下“停止”时再次显示系统栏。我正在使用这些方法:

fun hideSystemUI(container: View) {
        WindowCompat.setDecorFitsSystemWindows(window, false)
        WindowInsetsControllerCompat(window, container).let { controller ->
            controller.hide(WindowInsetsCompat.Type.systemBars())
            controller.systemBarsBehavior =
                WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        }
    }

    fun showSystemUI(container: View) {
        WindowCompat.setDecorFitsSystemWindows(window, true)
        WindowInsetsControllerCompat(window, container)
            .show(WindowInsetsCompat.Type.systemBars())
    }

但是当我进入 in/out 沉浸模式时,我的视图正在调整大小。由于父系统栏,我的限制调整大小似乎不再出现在屏幕上。我尝试使用插图:

fun View.applyPaddingByInsets(
    applyLeft: Boolean = false,
    applyTop: Boolean = false,
    applyRight: Boolean = false,
    applyBottom: Boolean = false
) {
    ViewCompat.setOnApplyWindowInsetsListener(this) { view, windowInsets ->
        val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
        view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
            val leftPadding = if (applyLeft) insets.left else 0
            val topPadding = if (applyTop) insets.top else 0
            val rightPadding = if (applyRight) insets.right else 0
            val bottomPadding = if (applyBottom) insets.bottom else 0

            view.updatePadding(leftPadding, topPadding, rightPadding, bottomPadding)
        }
        windowInsets
    }
}

我将它应用于 Constraint 中的第一个顶部元素,它是其余视图的父元素(它们连接到它的底部)并将它应用于我的最后一个(底部)视图,因此我的中间视图连接到顶部它的。但结果是一样的。视图仍在调整大小。你能帮我解决这个问题吗?我希望我的观点保持沉浸模式in/out不变

我找到了解决方案。由于文档的剪切最佳实践:https://developer.android.com/guide/topics/display-cutout#best_practices_for_display_cutout_support,我们需要在 LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVERLAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES 中设置 window.attributes.layoutInDisplayCutoutMode。也可以通过 windowInsets.getInsetsIgnoringVisibility(WindowInsetsCompat.Type.systemBars())

获取插图