4

我想使用带有默认警报对话框的新材料轮廓按钮。

我在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 Components 主题来设置 Yes 和 No 按钮的样式。

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

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

但输出如下 在此处输入图像描述

有没有办法使用带有默认警报对话框的最新材料概述按钮?如果有任何区别,我正在使用设计支持库中的材料组件。

4

1 回答 1

2

由于您使用的是材质主题,因此您可以使用:

      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>

在此处输入图像描述

于 2019-09-21T20:24:12.763 回答