美文网首页
compose横竖屏切换

compose横竖屏切换

作者: yunhen | 来源:发表于2023-12-26 18:09 被阅读0次

manifest里的配置的自己查一下吧,这里只贴啦方法的代码

@Composable
fun ScreenOrientation(){

    //获取当前屏幕状态
    val configuration = LocalConfiguration.current
    //获取activity
    val activity = (LocalContext.current as ComponentActivity)

    LaunchedEffect(configuration.orientation) {
        //横屏时状态栏隐藏并透明
        if (configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
            //状态栏显示
            activity.window.insetsController?.apply {
                systemBarsBehavior = android.view.WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
                show(android.view.WindowInsets.Type.systemBars())

            }
            showToast(activity,"||竖屏||")
        } else {

            //自动隐藏状态栏,下拉出现后过一段时间自动隐藏
            activity.window.insetsController?.apply {
                systemBarsBehavior = android.view.WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
                hide(android.view.WindowInsets.Type.systemBars())

            }
            showToast(activity,"--横屏--")
        }
    }
}

相关文章

网友评论

      本文标题:compose横竖屏切换

      本文链接:https://www.haomeiwen.com/subject/ajwlndtx.html