Android 没有扩展的 MaterialAlertDialog Theme.MaterialComponent

Android MaterialAlertDialog without extending Theme.MaterialComponent

我正在扩展 'Theme.AppCompat.Light.NoActionBar',我不想将我的 AppTheme 切换到 MaterialDesign

MaterialAlertDialogBuilder(context, R.style.MaterialDialog)
            .setCancelable(false)
            .setMessage(message)
            .setPositiveButton("OK") { dialog, _ -> dialog.dismiss() }
            .show()
<style name="MaterialDialog" parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="colorSurface">#ffffff</item>
    </style>

预计

结果:

如果您不能移动到 Material 组件主题,您应该使用 Bridge theme。类似于:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
    <item name="colorSurface">#ffffff</item>
    ...
</style>

然后在您的代码中使用:

MaterialAlertDialogBuilder(context)
   .setCancelable(false)
   ...

如果您只想覆盖对话框的颜色,只需使用:

MaterialAlertDialogBuilder(context,R.style.MaterialDialog)
   .setCancelable(false)
   ...

与:

  <style name="MaterialDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="materialThemeOverlay">@style/AlertDialogOverlay</item>
  </style>  


  <style name="AlertDialogOverlay">
    <item name="colorOnSurface">@color/....</item>
    <item name="colorSurface">#ffffff</item>
    <item name="colorPrimary">@color/.....</item>
    ....
  </style>