前言
相信大部分安卓开发者都比较熟悉setSystemUIVisibilty,通过传入对应的Flag实现实现状态栏沉浸式或者对状态栏、导航栏图标进行反色。但是从Android API 30开始,setSystemUIVisibilty就不被推荐使用 。接下来本文就介绍一下如何使用WindowInsetsController:
使用AndroidX接口实现
1。在AndroidX中,提供了一种方式让用户快速获取WindowInsetsController。首先你需要导入AndroidX core依赖(>=1.5.0):
implementation 'androidx.core:core-ktx:1.5.0'
2.示例
var controller = ViewCompat.getWindowInsetsController(window.decorView)
// 设置状态栏反色
controller?.isAppearanceLightStatusBars = true
// 取消状态栏反色
controller?.isAppearanceLightStatusBars = false
// 设置导航栏反色
controller?.isAppearanceLightNavigationBars = true
// 取消导航栏反色
controller?.isAppearanceLightNavigationBars = false
// 隐藏状态栏
controller?.hide(WindowInsets.Type.statusBars())
// 显示状态栏
controller?.show(WindowInsets.Type.statusBars())
// 隐藏导航栏
controller?.hide(WindowInsets.Type.navigationBars())
// 显示导航栏
controller?.show(WindowInsets.Type.navigationBars())
// 同时隐藏状态栏和导航栏
controller?.hide(WindowInsets.Type.systemBars())
// 同时隐藏状态栏和导航栏
controller?.show(WindowInsets.Type.systemBars())
本文作者:苏lisong
本文链接:https://sulisong.cn/index.php/archives/15/
最后修改时间:2021-07-26 03:19:20
本站未注明转载的文章均为原创,并采用 CC BY-NC-SA 4.0 授权协议,转载请注明来源,谢谢!