gpt4 book ai didi

android - 如何在方向改变时保持沉浸式模式?

转载 作者:行者123 更新时间:2023-11-29 02:32:23 25 4
gpt4 key购买 nike

目前在 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
}
}

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

提前谢谢你。

最佳答案

当我使用文档中的内容时,我遇到了类似的问题,只是 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)
}

关于android - 如何在方向改变时保持沉浸式模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48845929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com