首页 > 解决方案 > 如何退出沉浸式模式并返回默认的 Android UI 布局?

问题描述

我正在尝试在我的一个片段中使用粘性沉浸式模式。然后当片段被销毁时,返回到默认的 UI 视图。

这是我的代码:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

    hideSystemUI()
}

override fun onDestroyView() {
    super.onDestroyView()
    showSystemUI()
}

private fun hideSystemUI() {
    activity?.window?.decorView?.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            // Set the content to appear under the system bars so that the
            // content doesn't resize when the system bars hide and show.
            or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            // Hide the nav bar and status bar
            or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            or View.SYSTEM_UI_FLAG_FULLSCREEN)
}

private fun showSystemUI() {
    activity?.window?.decorView?.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
}

当我进入片段时,粘性沉浸模式有效,但是当它被破坏时,状态栏和应用栏之间存在间隙。它看起来像这样:

在此处输入图像描述

应用栏还覆盖了屏幕上的一些文本视图。当片段被销毁时,如何正确退出粘性沉浸模式并将其恢复为默认行为?

标签: android-fullscreenandroid-immersive

解决方案


推荐阅读