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