android.view.View.systemUiVisibility 已弃用.什么是替代品? [英] android.view.View.systemUiVisibility deprecated. What is the replacement?

查看:196
本文介绍了android.view.View.systemUiVisibility 已弃用.什么是替代品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将项目目标 API 版本更新为 30,现在我看到 systemUiVisibility 属性已被弃用.

I have updated the project target API version to 30, and now I see that the systemUiVisibility property is deprecated.

以下 kotlin 代码是我正在使用的代码,它实际上等效于 setSystemUiVisibility 方法.

The following kotlin code is the one I'm using which is actually equivalent to setSystemUiVisibility method in Java.

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)

如果您有此弃用代码的稳定替代品,请告诉我.google 的建议是使用 WindowInsetsController,但我不知道怎么做.

Please let me know if you have got any stable replacement for this deprecated code. The google's recommendation is to use WindowInsetsController, but I don't how to do that.

推荐答案

为了兼容性,请使用 WindowCompatWindowInsetsControllerCompat.您需要将 androidx.core 的 gradle 依赖项升级到至少 1.6.0-alpha03,以便支持 setSystemBarsBehavior在 SDK <30.

For compatibility, use WindowCompat and WindowInsetsControllerCompat. You'll need to upgrade your gradle dependency for androidx.core to at least 1.6.0-alpha03 so that there will be support for setSystemBarsBehavior on SDK < 30.

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 视频了解有关 WindowInsets 的更多信息

You can find out more information about WindowInsets by watching this YouTube video

对于在显示屏顶部有凹槽的设备,您可以将以下内容添加到 v27 theme.xml 文件中,使 UI 出现在凹槽的任一侧:

For devices with notches at the top of the display, you can add the following to your v27 theme.xml file make the UI appear either side of the notch:

<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>

您可以在此链接中阅读更多信息:Display Cutout

You can read more at this link: Display Cutout

这篇关于android.view.View.systemUiVisibility 已弃用.什么是替代品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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