我已经将项目目标应用编程接口版本更新为30,现在我看到systemUiVisibility属性已被弃用。
下面的kotlin代码就是我正在使用的代码,它实际上相当于Java语言中的setSystemUiVisibility方法。
playerView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LOW_PROFILE
or View.SYSTEM_UI_FLAG_FULLSCREEN
or 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_HIDE_NAVIGATION)
请让我知道,如果你有任何稳定的替代这个过时的代码。谷歌的建议是使用WindowInsetsController
,但我不知道该怎么做。
发布于 2020-11-14 05:08:57
为了兼容性,请使用WindowCompat
和WindowInsetsControllerCompat
。您需要将androidx.core
的gradle依赖项至少升级到1.6.0-alpha03
,以便在SDK < 30上支持setSystemBarsBehavior
。
private fun hideSystemUI() {
WindowCompat.setDecorFitsSystemWindows(window, false)
WindowInsetsControllerCompat(window, mainContainer).let { controller ->
controller.hide(WindowInsetsCompat.Type.systemBars())
controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
}
private fun showSystemUI() {
WindowCompat.setDecorFitsSystemWindows(window, true)
WindowInsetsControllerCompat(window, mainContainer).show(WindowInsetsCompat.Type.systemBars())
}
您可以通过观看此YouTube video了解有关WindowInsets
的更多信息
对于在显示屏顶部有凹槽的设备,您可以将以下内容添加到v27 theme.xml
文件中,使UI显示在凹槽的任一侧:
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
你可以在这个链接上阅读更多内容:Display Cutout
发布于 2020-10-06 15:43:37
TL;DR代码段
封装if-else结构,以避免旧版本的SDK出现java.lang.NoSuchMethodError: No virtual method setDecorFitsSystemWindows
异常。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.setDecorFitsSystemWindows(false)
} else {
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
}
有关Android 11中的嵌入和全屏模式的完整信息的链接
https://blog.stylingandroid.com/android11-windowinsets-part1/
发布于 2020-06-25 23:01:18
我希望它能对你有所帮助。
以前,当实现边缘到边缘导航或沉浸式模式时,要采取的第一步是使用systemUiVisibility标志来请求应用程序全屏布局,这个新的安卓版本不推荐使用这个字段,为了全屏布局应用程序,你必须在Window
类上使用一个新方法:setDecorFitsSystemWindows
将false
作为参数传递如下。
window.setDecorFitsSystemWindows(false)
WindowInsetsController
类,允许您执行以前通过systemUiVisibility
标志控制的操作,如隐藏或显示状态栏或导航栏(分别隐藏和显示方法)
例如,您可以轻松地显示和隐藏键盘,如下所示:
// You have to wait for the view to be attached to the
// window (otherwise, windowInsetController will be null)
view.doOnLayout {
view.windowInsetsController?.show(WindowInsets.Type.ime())
// You can also access it from Window
window.insetsController?.show(WindowInsets.Type.ime())
}
https://stackoverflow.com/questions/62577645
复制相似问题