【问题标题】:Jetpack compose layout changes in dialog doesn't update the size对话框中的 Jetpack 撰写布局更改不会更新大小
【发布时间】:2021-07-21 12:41:35
【问题描述】:

我有一个对话框,用户可以在其中选择某些内容,并且根据选择,布局将被更新。问题是对话框的高度永远不会更新以反映布局的变化。

如何重构对话框以使布局适合对话框?

例子:

@Composable
fun AlertDialogTest() {
    var showDialog by remember { mutableStateOf(false)}
    var showExtra by remember { mutableStateOf(false)}
    Button(onClick = { showDialog = true }) {
        Text("Open dialog")
    }
    if (showDialog) {
        AlertDialog(
            text = {
                Column { Button(onClick = {showExtra = true}) {
                        Text ("Show rest of dialog")
                    }
                    if (showExtra) {
                        Text("More text", style = MaterialTheme.typography.h5)
                        Text("Even more text", style = MaterialTheme.typography.h5)
                    }
                }
            },
            confirmButton = { TextButton(onClick = { showDialog = false }) {
                Text("Close")
            }},
            onDismissRequest = {showDialog = false},
        )
    }
}

结果:

【问题讨论】:

标签: android-jetpack-compose


【解决方案1】:

它在beta08 中对我来说确实有效。然后,在升级到rc02 后它停止工作 - 弹出对话框、下拉菜单(基本上是平台窗口支持的所有元素)停止在内容大小更改时正确调整大小。事实上,我发现了一个错误 - https://issuetracker.google.com/issues/194911971。遗憾的是,目前我还没有找到解决方法,所以我们最好等到它修复后。

UPD。 正如在上述问题中所评论的,有一种解决方法。检查答案here

【讨论】:

    【解决方案2】:

    我也遇到了同样的问题,在Jetpack Compose Alert Dialog找到了较长文本或其他内容的解决方案

     AlertDialog(
        onDismissRequest = { },
        properties = DialogProperties(usePlatformDefaultWidth = false),
        modifier = Modifier
            .padding(28.dp)
            .fillMaxWidth()
            .wrapContentHeight(),
        title = null,
        text = {
               // Your alert dialog content
        },confirmButton = {
            TextButton(onClick = { /*TODO*/ }) {
                Text(text = "OK")
            }
        })
    

    【讨论】:

    • 谢谢,我也用 .wrapContentHeight() 解决了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多