将最新的材料概述按钮与AlertDialog一起使用 [英] Using latest material outlined buttons with alertdialog

本文介绍了将最新的材料概述按钮与AlertDialog一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在默认警报对话框中使用新的材料轮廓按钮.

I want to use new material outlined buttons with default alert dialog.

我已经在 style.xml 中创建了样式,如下所示

I have created style in style.xml as below

<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>

我正在使用新的材料组件"主题来设置是"和否"按钮的样式.

I am using new Material Components theme to style Yes and No Buttons.

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

Now I use above style in my code by setting it to AlertDialog builder.

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

但是输出如下

是否可以在默认警报对话框"中使用最新的材料轮廓按钮?如果有任何区别,我正在使用设计支持库中的Material组件.

Is there a way to use latest material outlined buttons with Default Alert Dialog? I am using Material components from design support library if it makes any difference.

推荐答案

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

Since you are using a Material Theme you can use:

      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>

这篇关于将最新的材料概述按钮与AlertDialog一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆