android 如何隐藏导航栏时,显示对话框jetpack合成?

xzlaal3s  于 2022-12-21  发布在  Android
关注(0)|答案(3)|浏览(174)

我把导航条藏在了喷气背包里。
但是,当我显示对话框时,导航栏也会显示。
我想在显示对话框时隐藏导航栏。
详情请看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
        }
    }
}

zbwhf8kr

zbwhf8kr1#

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

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
nlejzf6q

nlejzf6q2#

我们还可以使用WindowsInsetsController 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
}
fiei3ece

fiei3ece3#

更新:看起来它仍然可以奇怪的行为在旧的Android版本
对话框与Activity位于不同的Window中,因此您需要从对话框内容中调用rememberSystemUiController来执行此操作。

@Composable
fun TutorialDialog(
    tutorial: Tutorial,
    onAck: () -> Unit
) {
    Dialog(
        properties = DialogProperties(
            dismissOnBackPress = false,
            dismissOnClickOutside = false,
            usePlatformDefaultWidth = false
        ),
        onDismissRequest = { }
    ) {
        TutorialView(
            tutorial = tutorial,
            onAck = onAck
        )
    }
}

@Composable
private fun TutorialView(tutorial: Tutorial, onAck: () -> Unit) {
    val systemUIController = rememberSystemUiController()
    systemUIController.isNavigationBarVisible = false
    systemUIController.isStatusBarVisible = false

    // composable code here
}

相关问题