【问题标题】:How to hide navigationbar when showing dialog in jetpack compose?在jetpack compose中显示对话框时如何隐藏导航栏?
【发布时间】:2022-10-16 21:45:30
【问题描述】:

我在 jetpack compose 中隐藏了导航栏。
但是,当我显示一个对话框时,导航栏也会显示。
我想在显示对话框时隐藏导航栏。
详情请看GIF动画

如果你有一个好主意,请告诉我。

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            SampleComposeTheme {
                var showDialog by remember { mutableStateOf(false) }
                OutlinedButton(
                    onClick = { showDialog = true }
                ) {
                    Text("Button")
                }

                if (showDialog) {
                    AlertDialog(
                        onDismissRequest = {},
                        text = {
                            Text("Alert")
                        },
                        confirmButton = {
                            Button(onClick = { showDialog = false }) {
                                Text("ConfirmButton")
                            }
                        }
                    )
                }
            }
        }

        window.insetsController?.apply {
            hide(WindowInsets.Type.navigationBars())
            systemBarsBehavior =
                WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        }
    }
}

【问题讨论】:

    标签: android kotlin android-alertdialog android-jetpack-compose


    【解决方案1】:

    您可以使用 Google 的 Accompanist 库,它提供了用于在 Compose 中更新系统 UI 栏的实用程序。Accompanist

    implementation "com.google.accompanist:accompanist-systemuicontroller:<version>"
    
    val systemUiController = rememberSystemUiController()
            systemUiController.isStatusBarVisible = false // Status bar
            systemUiController.isNavigationBarVisible = false // Navigation bar
            systemUiController.isSystemBarsVisible = false // Status & Navigation bars
            systemUiController.navigationBarDarkContentEnabled =false
    

    【讨论】:

    • 即使它在“活动”屏幕上工作,它也不能使用可组合的材料“对话框”工作。至少不在这里:(
    【解决方案2】:

    我们可以额外使用 WindowInsetsController for SDK > 30,必须使用 IF 或 @Requires 添加 SDK 检查

    window.insetsController?.apply {
        hide(WindowInsets.Type.navigationBars())
        systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE //keeps hidden and on swipe transparent bars are shown
    }
    

    【讨论】:

      猜你喜欢
      • 2016-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-29
      • 2019-02-25
      • 1970-01-01
      相关资源
      最近更新 更多