无法更改 Android 警报对话框按钮颜色

Can't change Android Alert Dialog Button Color

尽管我尝试了以下代码,但无论如何我都无法更改红色的警告对话框的颜色。

Java:

    private void showAlertDialog(String title, String message){
    AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this, R.style.AlertDialogTheme);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.setPositiveButton("Tamam", null);
    builder.show();
}

XML:

    <style name="AlertDialogTheme" parent="Theme.AppCompat.Dialog.Alert">
    <!--item name="android:background">@color/google_orange</item-->
    <item name="colorPrimary">@color/google_orange</item>
    <item name="buttonBarPositiveButtonStyle">@color/google_orange</item>
    <item name="colorButtonNormal">@color/google_orange</item>
    <item name="colorError">@color/google_orange</item>
    <item name="colorAccent">@color/google_orange</item>
</style>

无法重现您发布的问题:“无法更改 Android 警报对话框按钮颜色”。 'colorButtonNormal' 的使用改变了按钮的颜色。

我认为这可行!!

     private void showAlertDialog(String title, String message){
      AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this, R.style.AlertDialogTheme);
      builder.setTitle(title);
      builder.setMessage(message);
      builder.setPositiveButton("Tamam", null);
      builder.setOnShowListener( new OnShowListener() {
        @Override
        public void onShow(...) {
            builder.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(COLOR_YOU_WANT);
         }
        });

      builder.show();
   }

好的,刚刚解决。问题是我使用 android.app.AlertDialog 而不是 androidx.appcompat.app.AlertDialog。我不知道为什么,但下面的代码不适用于第一个。 如果你使用第二个,这就可以了:

<style name="AlertDialogTheme" parent="Theme.AppCompat.Dialog.Alert">
    <item name="colorAccent">@color/google_orange</item>
</style>

Java 代码与问题中的相同。