当我们在 Jetpack Compose 中显示
Dialog
/AlertDialog
时,背景似乎有点暗,有没有办法调整背景 Alpha 或使其透明?
例如:当显示对话框时,该图像中的白色背景变成深灰色。
你可以像这样轻松制作:
Dialog(onDismissRequest = {}) {
(LocalView.current.parent as DialogWindowProvider)?.window?.setDimAmount(0f)
// dialog content here...
}
我尝试使用Dialog,但无法清除标志WindowManager.LayoutParams.FLAG_DIM_BEHIND。
你可以尝试使用 Popup 来代替 Dialog,一切对我来说都很好。
Popup(
onDismissRequest = {},
properties = PopupProperties(
focusable = true,
dismissOnBackPress = false,
dismissOnClickOutside = false,
excludeFromSystemGesture = true,
)
) {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.fillMaxSize()
.background(Color.Transparent)
) {
// Your content code is here
}
}
您可以使用全屏对话框,然后将带有所需文本的卡片插入其中。例如:
AlertDialog(
modifier = Modifier.fillMaxSize(),
backgroundColor = Color.White.copy(alpha = 0.2f),
properties = DialogProperties(usePlatformDefaultWidth = false),
您可以在文本部分添加您的卡片组合:
text = {
Card(Modifier.size(200.dp)) {
Text(text = "test")
}
}
希望这有帮助!
此行为由
android.view.Window
控制。
它位于
Dialog
的深处,我能想到的改变它的唯一方法就是复制所有源代码。
然后,在源代码的这一行中,您可以插入以下内容:
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
如果你想设置透明背景,将
decorFitsSystemWindows
设置为 false 似乎非常可行。
Dialog(
onDismissRequest = { /*TODO*/ },
properties = DialogProperties(decorFitsSystemWindows = false)
) {
}