Jetpack Compose AlertDialog 内容布局大小
Jetpack Compose AlertDialog content layout size
我使用 Jetpack Compose 创建了对话框。但是,我将对话框项目的大小设置为 300dp 并将其对齐到中心,但我的项目被裁剪了,它不起作用。我认为出现此错误是因为无法指定列的大小,即项目的布局。我该如何解决?
这是我的对话来源:
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun ExceptionDialog(visible: MutableState<Boolean>, exception: Exception) {
if (visible.value) {
val context = LocalContext.current
val contents = listOf("눈덩이", "돌덩이", "나뭇가지", "새똥", "나뭇잎", "흙더미")
val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.something_error))
AlertDialog(
modifier = Modifier.size(350.dp),
onDismissRequest = { visible.value = false },
properties = DialogProperties(usePlatformDefaultWidth = false),
confirmButton = {
exception.message?.let { exceptionMessage ->
OutlinedButton(onClick = { Util.copy(context, exceptionMessage) }) {
Text(text = "에러 복사")
}
}
},
text = {
Column(
modifier = Modifier.size(300.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
LottieAnimation(
modifier = Modifier.size(200.dp),
iterations = LottieConstants.IterateForever,
composition = composition,
)
Text(
text = "깃메봇이가 예상치 못한 ${contents.random()}에 맞았어요 \uD83E\uDD72\n${exception.message}",
textAlign = TextAlign.Center,
color = Color.Black
)
}
},
shape = RoundedCornerShape(30.dp)
)
}
}
这是结果屏幕:
看起来 AlertDialog
在 text
块中找到第一个 Text
时对齐视图,因此您的视图发生了变化。
我不确定这是一个错误,还是不应该以这种方式使用。
好消息是您可以将动画视图放入 title
。这将以与 text
相同的方式工作,这意味着您只能在 title
和 text
中拥有自定义视图 或 Text
.
@Composable
fun ExceptionDialog(visible: MutableState<Boolean>, exception: Exception) {
if (visible.value) {
val contents = listOf("눈덩이", "돌덩이", "나뭇가지", "새똥", "나뭇잎", "흙더미")
val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.something_error))
AlertDialog(
onDismissRequest = { visible.value = false },
properties = DialogProperties(usePlatformDefaultWidth = false),
confirmButton = {
exception.message?.let { exceptionMessage ->
OutlinedButton(onClick = { }) {
Text(text = "에러 복사")
}
}
},
title = {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier.fillMaxWidth()
) {
LottieAnimation(
iterations = LottieConstants.IterateForever,
composition = composition,
modifier = Modifier
.size(200.dp)
)
}
},
text = {
Text(
text = "깃메봇이가 예상치 못한 ${contents.random()}에 맞았어요 \uD83E\uDD72\n${exception.message}",
textAlign = TextAlign.Center,
color = Color.Black,
modifier = Modifier.fillMaxWidth()
)
},
shape = RoundedCornerShape(30.dp)
)
}
}
我使用 Jetpack Compose 创建了对话框。但是,我将对话框项目的大小设置为 300dp 并将其对齐到中心,但我的项目被裁剪了,它不起作用。我认为出现此错误是因为无法指定列的大小,即项目的布局。我该如何解决?
这是我的对话来源:
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun ExceptionDialog(visible: MutableState<Boolean>, exception: Exception) {
if (visible.value) {
val context = LocalContext.current
val contents = listOf("눈덩이", "돌덩이", "나뭇가지", "새똥", "나뭇잎", "흙더미")
val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.something_error))
AlertDialog(
modifier = Modifier.size(350.dp),
onDismissRequest = { visible.value = false },
properties = DialogProperties(usePlatformDefaultWidth = false),
confirmButton = {
exception.message?.let { exceptionMessage ->
OutlinedButton(onClick = { Util.copy(context, exceptionMessage) }) {
Text(text = "에러 복사")
}
}
},
text = {
Column(
modifier = Modifier.size(300.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
LottieAnimation(
modifier = Modifier.size(200.dp),
iterations = LottieConstants.IterateForever,
composition = composition,
)
Text(
text = "깃메봇이가 예상치 못한 ${contents.random()}에 맞았어요 \uD83E\uDD72\n${exception.message}",
textAlign = TextAlign.Center,
color = Color.Black
)
}
},
shape = RoundedCornerShape(30.dp)
)
}
}
这是结果屏幕:
看起来 AlertDialog
在 text
块中找到第一个 Text
时对齐视图,因此您的视图发生了变化。
我不确定这是一个错误,还是不应该以这种方式使用。
好消息是您可以将动画视图放入 title
。这将以与 text
相同的方式工作,这意味着您只能在 title
和 text
中拥有自定义视图 或 Text
.
@Composable
fun ExceptionDialog(visible: MutableState<Boolean>, exception: Exception) {
if (visible.value) {
val contents = listOf("눈덩이", "돌덩이", "나뭇가지", "새똥", "나뭇잎", "흙더미")
val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.something_error))
AlertDialog(
onDismissRequest = { visible.value = false },
properties = DialogProperties(usePlatformDefaultWidth = false),
confirmButton = {
exception.message?.let { exceptionMessage ->
OutlinedButton(onClick = { }) {
Text(text = "에러 복사")
}
}
},
title = {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier.fillMaxWidth()
) {
LottieAnimation(
iterations = LottieConstants.IterateForever,
composition = composition,
modifier = Modifier
.size(200.dp)
)
}
},
text = {
Text(
text = "깃메봇이가 예상치 못한 ${contents.random()}에 맞았어요 \uD83E\uDD72\n${exception.message}",
textAlign = TextAlign.Center,
color = Color.Black,
modifier = Modifier.fillMaxWidth()
)
},
shape = RoundedCornerShape(30.dp)
)
}
}