Using latest material outlined buttons with alertdialog

假如想象 提交于 2019-12-23 17:54:37

问题


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

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.

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

But the output is as below

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.


回答1:


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();

And then define the style:

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



来源:https://stackoverflow.com/questions/53429068/using-latest-material-outlined-buttons-with-alertdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!