使用 androidx DialogFragment 创建 AlertDialog 时按钮样式错误

Wrong button style when creating AlertDialog with androidx DialogFragment

这是我的根样式:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

这是我创建对话框的方式:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

        return AlertDialog.Builder(requireActivity())
            .apply {

                setMessage(R.string.dialog_delete_service_message)
                setPositiveButton(
                    R.string.dialog_delete_service_positive_button
                ) { _, _ ->
                    listener?.onConfirmed()
                }
                setNegativeButton(R.string.dialog_delete_service_negative_button, null)

            }.create()
    }

片段是 androidx.fragment.app.DialogFragment 的子类。

对话框显示如下:

我想显示无边框按钮,但对话框按钮有边框。是否缺少任何样式?

使用 MaterialAlertDialogBuilder

new MaterialAlertDialogBuilder(context)
            .setTitle("xxxx")
            .setMessage("...")
            .setPositiveButton("..", null)
            .show();

并在您的应用主题中使用此样式:

 <!-- Base application theme. -->
 <style name="AppTheme" parent="Theme.MaterialComponents.Light">
    ...
    <item name="materialAlertDialogTheme">@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog</item>
 </style>