如何修改 AlertDialog 背景黑色的透明度
时间: 2023-08-10 22:05:24 浏览: 82
要修改 `AlertDialog` 背景黑色的透明度,你可以在自定义主题中设置 `colorBackgroundFloating` 属性为一个带透明度的颜色值。具体来说,你可以使用带 `alpha` 值的 ARGB 颜色来控制黑色的透明度,例如 `#80000000` 表示黑色的透明度为 50%。
以下是一个示例主题,将 `colorBackgroundFloating` 属性设置为具有 50% 透明度的黑色:
```xml
<style name="MyAlertDialogTheme" parent="Theme.MaterialComponents.Light.Dialog.Alert">
<item name="colorBackgroundFloating">#80000000</item>
</style>
```
在这个例子中,我们将 `colorBackgroundFloating` 属性设置为 `#80000000`,即黑色的透明度为 50%。你可以将这个主题应用到 `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` 对象时,该样式将被应用。
阅读全文
相关推荐















