我正在尝试使我的android应用在全屏和非全屏模式之间切换。根据其他堆栈溢出的答案,我已经对该扩展进行了扩展,可以完成我需要的大部分工作...
fun AppCompatActivity?.toggleFullscreen(fullscreen: Boolean) {
if (this == null) return
val attrs = window.attributes
if (fullscreen) {
attrs.flags = attrs.flags or WindowManager.LayoutParams.FLAG_FULLSCREEN
} else {
attrs.flags = attrs.flags and WindowManager.LayoutParams.FLAG_FULLSCREEN.inv()
}
window.attributes = attrs
}
但这仍然保留导航栏在屏幕底部,我尝试添加https://developer.android.com/training/system-ui/immersive建议的其他标志(通过放置另一个标志或在全屏标志之后),包括:
SYSTEM_UI_FLAG_HIDE_NAVIGATION
SYSTEM_UI_FLAG_IMMERSIVE_STICKY
但似乎没有一个起作用,我缺少什么吗?
答案 0 :(得分:1)
您可以使用此方法调用全屏切换
fun toggleFullScreen() {
if (window.decorView.systemUiVisibility == View.SYSTEM_UI_FLAG_VISIBLE) {
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_FULLSCREEN
} else {
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
}
}
有关更多详细信息,请参考此链接https://developer.android.com/training/system-ui/immersive,有关标志的详细信息,请参考此链接https://developer.android.com/reference/android/view/View.html#SYSTEM_UI_FLAG_VISIBLE