不确定我是否遗漏了一些明显的东西,但是当显示对话框时,我无法在父屏幕中获取灰色叠加背景。使用此代码创建一个全新的应用程序:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
TestsTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Column(modifier = Modifier.fillMaxSize()) {
Greeting("Android")
}
CustomDialog()
}
}
}
}
}
@Composable
fun CustomDialog() {
var showDialog by remember { mutableStateOf(true) }
if (showDialog) {
Dialog(onDismissRequest = {
showDialog = !showDialog
}) {
Column(modifier = Modifier.fillMaxWidth()
.background(color = Color.Red)) {
Text(text = "Hello Dialog!")
Text(text = "Subtitle!")
}
}
}
}
我发现的所有文档和示例都显示了背景中带有灰色覆盖层的模式。有没有我缺少的设置来显示它?
此行为仅在 Android 模拟器中运行时发生。我不会假装知道为什么,但我也浪费了很多时间试图“添加”背景变暗......只是最终发现这不是我做错的事情。