val systemUiController = rememberSystemUiController() //控制状态栏
val configuration = LocalConfiguration.current //获取当前屏幕状态
val activity =(LocalContext.current as Activity) //获取activity
LaunchedEffect(configuration.orientation) {
//横屏时状态栏隐藏并透明
if (configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
systemUiController.isSystemBarsVisible = true //状态栏可见
} else {
//systemUiController.isSystemBarsVisible = false
//systemUiController.setSystemBarsColor(Color.Transparent)
//自动隐藏状态栏,下拉出现后过一段时间自动隐藏
activity.window.insetsController?.apply {
systemBarsBehavior=WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
hide(WindowInsets.Type.systemBars())
}
}
}
在Composable函数中根据屏幕状态隐藏或者显示状态栏
//切换横屏
activity.requestedOrientation =ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
//切换竖屏
activity.requestedOrientation =ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED