0

我有一个带有片段容器的活动。另外,我有三个片段FragmentA, FragmentB, FragmentC

当我导航到时,FragmentC我希望半透明状态栏与内容重叠。在所有其他情况下,当所有内容都在状态栏下(没有重叠)时,我想使用默认外观。为此,我将我的 AppTheme 修改为:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@android:color/white</item>
    <item name="android:statusBarColor">@color/transparent</item>

    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">false</item>
</style>

另外,我已经实现了这样的工具来动态改变外观的“模式”:

data class WindowSettings(
    val fitSystemWindowsForStatusBar: Boolean
)

private fun BaseFragment.windowSettingsForFragment(): WindowSettings {
    return when (this) {
        is FragmentC -> WindowSettings(fitSystemWindowsForStatusBar = false)
        else -> WindowSettings(fitSystemWindowsForStatusBar = true)
    }
}

private fun BaseFragment.applyWindowSettings(settings: WindowSettings) {
    if (settings.fitSystemWindowsForStatusBar) {
        @Suppress("Deprecation")
        requireActivity().window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    } else {
        @Suppress("Deprecation")
        requireActivity().window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    }
}

fun BaseFragment.retrieveWindowSettings(): () -> Unit {
    return {
        applyWindowSettings(windowSettingsForFragment())
    }
}

onResume我的BaseFragment我用它来改变“模式”:

override fun onResume() {
        super.onResume()
        retrieveWindowSettings().invoke()
}

它运作良好,但我想避免使用已弃用的功能。我知道,弃用的替代品window.decorView.systemUiVisibility是使用WindowInsetsController. 但我不知道如何将它用于我的目的,因为我只找到了隐藏状态栏的解决方案,如下所示:

WindowInsetsControllerCompat(window, mainContainer).let { controller ->
        controller.hide(WindowInsetsCompat.Type.systemBars())
        controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}

所以,我的问题是制作的替代品是什么systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

4

0 回答 0