将最新的 material 轮廓按钮与警报对话框一起使用

Using latest material outlined buttons with alertdialog

我想在默认警报对话框中使用新的 material 轮廓按钮。

我在style.xml中创建了如下样式

<style name="OutlinedButton" parent="Widget.MaterialComponents.Button.TextButton">
    <item name="strokeColor">@color/colorAccent</item>
    <item name="strokeWidth">2dp</item>
</style>

<style name="MaterialDialogStyle" parent="Theme.MaterialComponents.Dialog.Alert">
    <item name="android:textColorPrimary">@color/colorAccent</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorPrimary">@color/colorAccent</item>
    <item name="buttonStyle">@style/OutlinedButton</item>
</style>

我正在使用新的 Material 组件主题来设置是和否按钮的样式。

现在我通过将其设置为 AlertDialog 构建器来在我的代码中使用上述样式。

AlertDialog.Builder builder = new AlertDialog.Builder(ProductListActivity.this, R.style.MaterialDialogStyle);

但是输出如下

有没有办法在默认警报对话框中使用最新的 material 轮廓按钮?我正在使用来自设计支持库的 Material 组件,如果它有任何不同的话。

由于您使用的是 Material 主题,您可以使用:

      new MaterialAlertDialogBuilder(MainActivity.this, R.style.MyMaterialAlertDialog)
          .setTitle("Clear cart")
          .setMessage("Pressing back...")
          .setPositiveButton("YES", null)
          .setNegativeButton("NO", /* listener = */ null)
          .show();

然后定义样式:

  <style name="MyMaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="buttonBarPositiveButtonStyle">@style/OutlinedButton</item>
    <item name="buttonBarNegativeButtonStyle">@style/OutlinedButton</item>
  </style>

  <style name="OutlinedButton" parent="Widget.MaterialComponents.Button.OutlinedButton">
    <item name="strokeColor">@color/colorAccent</item>
    <item name="strokeWidth">2dp</item>
  </style>