如何在方向改变时保持沉浸式模式? [英] How to preserve immersive mode on orientation change?

查看:104
本文介绍了如何在方向改变时保持沉浸式模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前使用Activity类中的以下代码块进入粘性沉浸式模式:

Currently using this block of code in Activity class to enter sticky immersive mode:

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
    }
}

切换方向后,状态栏会返回(甚至不透明)并停留直到被拖动,然后再次消失.我不太了解这种行为的原因,该如何解决?

When orientation is switched, the status bar comes back (not even translucent) and stays until dragged, then disappears again. I don't really understand the reason for this behavior, how do I fix it?

谢谢.

推荐答案

当我使用文档中的内容时,我遇到了类似的问题,仅使用onWindowFocusChanged尝试了hideSystemUi.切换到横向模式时,它只能在纵向模式下使用.

I faced similar issue when i used what was in the documentation, with just onWindowFocusChanged i tried to hideSystemUi. it worked only on portrait mode when switching to landscape it used to break.

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

In the documentation i also found OnSystemUiVisibilityChangeListener this will get a callback when system ui is visible here you can check if SYSTEM_UI_FLAG_FULLSCREEN == 0 and again call hideSystemUi().

这在风景和肖像上都对我有用:

This worked for me both on landscape and portrait:

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)
}

这篇关于如何在方向改变时保持沉浸式模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆