这方面网上的资料很乱,简单来说就三步:
首先定义?
fun Activity.toggleFullScreen(fullScreen: Boolean) {
val windowInsetsController = window.insetsController
if (fullScreen) {
val params = window.attributes
params.layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
window.attributes = params
windowInsetsController?.systemBarsBehavior =
WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE // 当用户从上往下滑动时,系统将短暂出现一个半透明的
windowInsetsController?.hide(WindowInsets.Type.statusBars())
windowInsetsController?.hide(WindowInsets.Type.systemBars())
windowInsetsController?.hide(WindowInsets.Type.navigationBars())
windowInsetsController?.hide(WindowInsets.Type.captionBar())
windowInsetsController?.hide(WindowInsets.Type.systemGestures())
} else {
val params = window.attributes
params.layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT
window.attributes = params
windowInsetsController?.systemBarsBehavior =
WindowInsetsController.BEHAVIOR_DEFAULT
windowInsetsController?.show(WindowInsets.Type.statusBars())
windowInsetsController?.show(WindowInsets.Type.systemBars())
windowInsetsController?.show(WindowInsets.Type.navigationBars())
windowInsetsController?.show(WindowInsets.Type.captionBar())
windowInsetsController?.show(WindowInsets.Type.systemGestures())
}
}
在 compose 函数中:
val Lcc = LocalContext.current
var isFullScreen by remember { mutableStateOf(false) }
LaunchedEffect(isFullScreen) {
val activity = Lcc as Activity
activity.toggleFullScreen(isFullScreen)
}
最后提供 UI 即可:
DropdownMenuItem(
text = { Text(if (isFullScreen) "正常模式" else "全屏模式") },
leadingIcon = { Icon(Icons.TwoTone.FitScreen, contentDescription = null) },
onClick = {
onDismiss()
isFullScreen = !isFullScreen
},
)
因为资料很乱,我也忘记参考资料是什么了,就先不贴了
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于