colorBackgroundFloating 用在 AlertDialog 的哪个部分
时间: 2024-01-23 19:03:19 浏览: 32
`colorBackgroundFloating` 属性用于定义 `AlertDialog` 的背景颜色,包括对话框的边框、标题以及按钮区域等。具体来说,`colorBackgroundFloating` 属性会影响到 `AlertDialog` 的以下部分:
- 对话框的背景(不包括内容区域);
- 对话框的边框;
- 对话框的标题栏;
- 对话框的按钮区域。
在使用 `AlertDialog` 时,你可以在主题中设置 `colorBackgroundFloating` 属性,以自定义对话框的背景颜色。例如:
```xml
<style name="MyAlertDialogTheme" parent="Theme.MaterialComponents.Light.Dialog.Alert">
<item name="colorBackgroundFloating">@color/my_alert_dialog_background</item>
</style>
```
在这个例子中,我们定义了一个名为 `MyAlertDialogTheme` 的主题,它继承自 `Theme.MaterialComponents.Light.Dialog.Alert` 主题。我们将 `colorBackgroundFloating` 属性设置为 `@color/my_alert_dialog_background`,以自定义 `AlertDialog` 的背景颜色。你可以将这个主题应用到 `AlertDialog` 上,例如:
```kotlin
val alertDialog = AlertDialog.Builder(this, R.style.MyAlertDialogTheme)
.setTitle("Title")
.setMessage("Message")
.setPositiveButton("OK") { dialog, which -> }
.setNegativeButton("Cancel") { dialog, which -> }
.create()
alertDialog.show()
```
在这个例子中,我们将 `MyAlertDialogTheme` 应用到 `AlertDialog.Builder` 中,以自定义对话框的样式。当我们调用 `create()` 方法创建 `AlertDialog` 对象时,该样式将被应用。