无法更改 AlertDialog 按钮的背景颜色

use*_*180 2 android android-appcompat android-alertdialog material-design material-components-android

我正在尝试使用可能在 stackoverflow 中为此问题定义的所有解决方案来更改 AlertDialog 的颜色,但它仍然不起作用。

这是我用来尝试实现的代码:

 val vacationDialog = AlertDialog.Builder(fragment.context,R.style.DialogTheme)
 val factory = LayoutInflater.from(OtrosDetailFragment.fragment.context);
 var view = factory.inflate(R.layout.sample, null)
 [...]
 vacationDialog.setView(view)
        val window = vacationDialog.create()
 vacationDialog.setPositiveButton("Cerrar"){dialog, which ->
            dialog.dismiss()

        }

 /**Listener called when the AlertDialog is shown**/


 vacationDialog.show()

 window.setOnShowListener {

 /**Get the positive button from the AlertDialog**/
 val positiveButton = window.getButton(DialogInterface.BUTTON_POSITIVE)

 /**Set your color to the positive button**/
 positiveButton.setBackgroundColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
 positiveButton.setTextColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
 positiveButton.setHintTextColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
 positiveButton.setLinkTextColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
        }
Run Code Online (Sandbox Code Playgroud)

这是styles.xml 中定义的DialogTheme 样式,顺便说一下,我找不到更改默认按钮颜色(黑色)的方法,并且似乎覆盖了从代码中尝试的任何更改。

<style name="DialogTheme" parent="android:Theme.Holo">

        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <!-- No backgrounds, titles or window float -->
        <item name="android:windowBackground">@color/white</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:gravity">center</item>


    </style>
Run Code Online (Sandbox Code Playgroud)

关于如何解决这个问题的任何想法?

Gab*_*tti 5

如果您使用的是AppCompat主题,则可以使用以下内容:

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <!-- Used for the buttons -->
        <item name="colorAccent">#FFCC00</item>
        <!-- Used for the title and text -->
        <item name="android:textColorPrimary">#FFFFFF</item>
        <!-- Used for the background -->
        <item name="android:background">#5fa3d0</item>
</style>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

使用适用于 android 库的新Material组件,您可以使用新的androidx.appcompat.app.AlertDialog.

只需使用类似的东西:

new MaterialAlertDialogBuilder(context)
            .setTitle("Dialog")
            .setMessage("Lorem ipsum dolor ....")
            .setPositiveButton("Ok", /* listener = */ null)
            .setNegativeButton("Cancel", /* listener = */ null)
            .show();
Run Code Online (Sandbox Code Playgroud)

您可以使用以下内容自定义主题:

  <!-- Alert Dialog -->
  <style name="MyThemeOverlay.MaterialComponents.MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
    <item name="buttonBarNegativeButtonStyle">@style/Widget.MaterialComponents.Button.TextButton.Dialog</item>
  </style>
Run Code Online (Sandbox Code Playgroud)

和:

  <style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">#FFFFFF</item>
    <item name="backgroundTint">@color/selector_bt</item>
  </style>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明