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

时间:2020-03-09 21:31:43

标签: android xml button colors android-alertdialog

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

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>

3 个答案:

答案 0 :(得分:1)

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

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

Java代码与问题中的代码相同。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我认为这会起作用!

     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();
   }
相关问题