当我们在Jetpack Compose中展示
Dialog
/AlertDialog
时,背景色似乎有点暗,有没有方法可以调整背景的透明度或使其透明?
例如:在此图像中,白色背景在显示对话框时变为深灰色。Dialog
/AlertDialog
时,背景色似乎有点暗,有没有方法可以调整背景的透明度或使其透明?
例如:在此图像中,白色背景在显示对话框时变为深灰色。Dialog(onDismissRequest = {}) {
(LocalView.current.parent as DialogWindowProvider)?.window?.setDimAmount(0f)
// dialog content here...
}
我曾尝试使用Dialog,但无法清除标志WindowManager.LayoutParams.FLAG_DIM_BEHIND。
你可以尝试使用弹出窗口(Popup)代替对话框,这样一切都能正常工作。
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
}
}
android.view.Window
控制。Dialog
中,想要更改它的唯一方法是复制所有源代码。window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
AlertDialog(
modifier = Modifier.fillMaxSize(),
backgroundColor = Color.White.copy(alpha = 0.2f),
properties = DialogProperties(usePlatformDefaultWidth = false),
text = {
Card(Modifier.size(200.dp)) {
Text(text = "test")
}
}
Dialog(
onDismissRequest = {
}
) {
val curView = LocalView.current
/* Change the transparency of the dialog window */
LaunchedEffect(curView) {
tailrec fun Context.findWindow(): Window? = when (this) {
is Activity -> window
is ContextWrapper -> baseContext.findWindow()
else -> null
}
fun View.findWindow(): Window? =
(parent as? DialogWindowProvider)?.window ?: context.findWindow()
try {
val window = curView.findWindow() ?: return@LaunchedEffect
val lp = window.attributes
lp.dimAmount = dimAmount // Modify the dim amount value
window.attributes = lp
} catch (e: Throwable) {
e.printStackTrace()
}
}
// content
}
decorFitsSystemWindows
设为false似乎是可行的。Dialog(
onDismissRequest = { /*TODO*/ },
properties = DialogProperties(decorFitsSystemWindows = false)
) {
}