如何在方向改变时保留沉浸模式?

Nic*_*ick 6 android screen-orientation kotlin android-immersive

目前在 Activity 类中使​​用此代码块来进入粘性沉浸模式:

override fun onWindowFocusChanged(hasFocus: Boolean) {
    super.onWindowFocusChanged(hasFocus)

    if (hasFocus && android.os.Build.VERSION.SDK_INT > 15) {
        var flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                View.SYSTEM_UI_FLAG_FULLSCREEN
        flags = if (android.os.Build.VERSION.SDK_INT < 19) flags
            else flags or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        window.decorView.systemUiVisibility = flags
    }
}
Run Code Online (Sandbox Code Playgroud)

当方向切换时,状态栏会回来(甚至不是半透明的)并保持不变,直到拖动,然后再次消失。我真的不明白这种行为的原因,我该如何解决它?

先感谢您。

Sid*_*h G 5

当我使用文档中的内容时,我遇到了类似的问题,仅使用 onWindowFocusChanged 我试图隐藏SystemUi。它只能在纵向模式下工作,当切换到横向模式时它就会崩溃。

在文档中,我还发现 OnSystemUiVisibilityChangeListener 当系统 ui 可见时,这将获得回调,您可以检查 SYSTEM_UI_FLAG_FULLSCREEN == 0 并再次调用 hideSystemUi()。

这对我来说无论是横向还是纵向都有效:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
window.decorView.setOnSystemUiVisibilityChangeListener { visibility ->
        // Note that system bars will only be "visible" if none of the
        // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
        if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
            hideSystemUI()
            // adjustments to your UI, such as showing the action bar or
            // other navigational controls.
        }
    }
}

override fun onWindowFocusChanged(hasFocus: Boolean) {
    super.onWindowFocusChanged(hasFocus)
    if (hasFocus) hideSystemUI()
}


private fun hideSystemUI() {
    window.decorView.systemUiVisibility = (
            // Set the content to appear under the system bars so that the
            // content doesn't resize when the system bars hide and show.
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    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)
}
Run Code Online (Sandbox Code Playgroud)


Pau*_*Chu 0

我认为隐藏工具栏的唯一方法

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    getSupportActionBar()?.hide()
    onWindowFocusChanged(true)
}


override fun onWindowFocusChanged(hasFocus: Boolean) {
    super.onWindowFocusChanged(hasFocus)

    if (hasFocus && android.os.Build.VERSION.SDK_INT > 15) {
        var flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                View.SYSTEM_UI_FLAG_FULLSCREEN
        flags = if (android.os.Build.VERSION.SDK_INT < 19) flags else
            flags or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        window.decorView.systemUiVisibility = flags
    }
}
Run Code Online (Sandbox Code Playgroud)