安卓沉浸式状态栏
2022-05-07 本文已影响0人
lirudong
当你升级到androidx后,将会向下兼容,我测试到安卓6.0以上都支持
首先添加依赖:
implementation 'androidx.core:core-ktx:1.6.0'
在你的activity中写上
WindowCompat.setDecorFitsSystemWindows(window,false)
然后你就能得到沉浸式状态栏了,如下

视频:https://v.qq.com/x/page/j33360wjcaq.html
修改状态栏字体颜色
添加代码:
val insetsController = WindowCompat.getInsetsController(window, window.decorView)
insetsController?.isAppearanceLightStatusBars = true //true:黑色 ,false:白色
赋值为true
时字体颜色为黑色

赋值为
false
时字体颜色为白色
隐藏状态栏
如果你想隐藏或显示状态栏
insetsController?.hide(WindowInsetsCompat.Type.statusBars())
insetsController?.show(WindowInsetsCompat.Type.statusBars())
调用hide()
方法为隐藏状态栏,效果如下
