安卓沉浸式状态栏

\color{red}{注意:}当你升级到androidx后,将会向下兼容,我测试到安卓6.0以上都支持
首先添加依赖:

implementation 'androidx.core:core-ktx:1.6.0'

在你的activity中写上

WindowCompat.setDecorFitsSystemWindows(window,false)

然后你就能得到沉浸式状态栏了,如下


Screenshot_20220507-092846.png

视频:https://v.qq.com/x/page/j33360wjcaq.html

修改状态栏字体颜色

添加代码:

val insetsController = WindowCompat.getInsetsController(window, window.decorView)
insetsController?.isAppearanceLightStatusBars = true  //true:黑色 ,false:白色

赋值为true时字体颜色为黑色

Screenshot_20220507-093519.png

赋值为false时字体颜色为白色
Screenshot_20220507-093815.png

隐藏状态栏

如果你想隐藏或显示状态栏

insetsController?.hide(WindowInsetsCompat.Type.statusBars())
insetsController?.show(WindowInsetsCompat.Type.statusBars())

调用hide()方法为隐藏状态栏,效果如下

Screenshot_20220507-094657.png

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容