如何在 android compose 中更改对话框的宽度
How to change the width of dialog in android compose
似乎无法使用 Compose 更改对话框的宽度。
我最接近更改对话框宽度的方法是通过 DialogProperties.usePlatformDefaultWidth
。将其设置为 false 会导致对话框填满屏幕,但有没有办法使用自定义宽度?
Use 可以使用带有 text
、title
和 buttons
参数的构造函数定义自定义 AlertDialog
并应用 size(例如使用 Modifier.size
)并使用 usePlatformDefaultWidth = false
覆盖默认行为:
AlertDialog(
onDismissRequest = { /*TODO*/ },
title = {
Text(text = "Title")
},
text = {
Text(
"This area typically contains the supportive text " +
"which presents the details regarding the Dialog's purpose."
)
},
buttons = {},
properties = DialogProperties(
usePlatformDefaultWidth = false
),
modifier = Modifier.size(200.dp,250.dp)
)
如果您想在所有项目中使用恒定宽度,您可以创建一个具有自定义宽度的对话框,如下所示
@Composable
fun MyCustomDialog(
onDismissRequest: () -> Unit,
properties: DialogProperties = DialogProperties(),
content: @Composable () -> Unit
) {
Dialog(
onDismissRequest = onDismissRequest,
// We are copying the passed properties
// then setting usePlatformDefaultWidth to false
properties = properties.let {
DialogProperties(
dismissOnBackPress = it.dismissOnBackPress,
dismissOnClickOutside = it.dismissOnClickOutside,
securePolicy = it.securePolicy,
usePlatformDefaultWidth = false
)
},
content = {
Surface(
color = Color.Transparent,
modifier = Modifier.width(250.dp), // Customize your width here
content = content
)
}
)
}
似乎无法使用 Compose 更改对话框的宽度。
我最接近更改对话框宽度的方法是通过 DialogProperties.usePlatformDefaultWidth
。将其设置为 false 会导致对话框填满屏幕,但有没有办法使用自定义宽度?
Use 可以使用带有 text
、title
和 buttons
参数的构造函数定义自定义 AlertDialog
并应用 size(例如使用 Modifier.size
)并使用 usePlatformDefaultWidth = false
覆盖默认行为:
AlertDialog(
onDismissRequest = { /*TODO*/ },
title = {
Text(text = "Title")
},
text = {
Text(
"This area typically contains the supportive text " +
"which presents the details regarding the Dialog's purpose."
)
},
buttons = {},
properties = DialogProperties(
usePlatformDefaultWidth = false
),
modifier = Modifier.size(200.dp,250.dp)
)
如果您想在所有项目中使用恒定宽度,您可以创建一个具有自定义宽度的对话框,如下所示
@Composable
fun MyCustomDialog(
onDismissRequest: () -> Unit,
properties: DialogProperties = DialogProperties(),
content: @Composable () -> Unit
) {
Dialog(
onDismissRequest = onDismissRequest,
// We are copying the passed properties
// then setting usePlatformDefaultWidth to false
properties = properties.let {
DialogProperties(
dismissOnBackPress = it.dismissOnBackPress,
dismissOnClickOutside = it.dismissOnClickOutside,
securePolicy = it.securePolicy,
usePlatformDefaultWidth = false
)
},
content = {
Surface(
color = Color.Transparent,
modifier = Modifier.width(250.dp), // Customize your width here
content = content
)
}
)
}