抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

在API30中,decorView的setSystemUiVisibility方法以及View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR等已经被废弃了,那么如何操作状态栏呢

查看Android的API文档,里面写了使用WindowInsetsController

1
2
This method was deprecated in API level 30.
SystemUiVisibility flags are deprecated. Use WindowInsetsController instead.

查看WindowInsetsController的文档,发现其中替代的方法是setSystemBarsAppearance

1
2
3
4
abstract fun setSystemBarsAppearance(
appearance: Int,
mask: Int
): Unit

控制systemBar的外观.

例如,下面的语句添加 :APPEARANCE_LIGHT_STATUS_BARS

1
setSystemBarsAppearance(APPEARANCE_LIGHT_STATUS_BARS, APPEARANCE_LIGHT_STATUS_BARS)

以下语句可以清除它:

1
setSystemBarsAppearance(0, APPEARANCE_LIGHT_STATUS_BARS)

我们实战使用一下,有些时候,页面是白色背景,白色的状态栏,状态栏图标默认白色,就会看不清图标,所以需要改成白底黑字的状态栏。如图所示:
Android

修改方法:使用WindowInsetsControllersetSystemBarsAppearance方法

1
2
3
4
5
val controller: WindowInsetsController = window.insetsController!!
controller.setSystemBarsAppearance(
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
)

修改之后:
Android

评论