更改AlertDialog按钮背景

时间:2014-11-17 13:43:48

标签: android button colors android-alertdialog

在我正在开发的应用程序中,我想要做的就是更改AlertDialog按钮的突出显示颜色。我的主要智能来源是this讨论,但我在StackOverflow上也阅读了很多关于此的文章。下面的代码运行时没有崩溃,但按钮的高亮颜色仍然是默认的橙黄色。有人知道出了什么问题吗?

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(message).setCancelable(true)
        .setPositiveButton(getResources().getString(R.string.positive_button_title), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                // Do stuff
            }
        });
    // Incredibly bulky way of simply changing the button highlight color,
    // but it appears to be the only way we can do it without getting a NullPointerException
    AlertDialog dialog = builder.create();
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialogInterface) {
            if (Build.VERSION.SDK_INT < 16) {
                ((AlertDialog) dialogInterface).getButton(AlertDialog.BUTTON_POSITIVE).setBackgroundDrawable(getResources().getDrawable(R.drawable.dialog_button_drawable));
            } else {
                ((AlertDialog) dialogInterface).getButton(AlertDialog.BUTTON_POSITIVE).setBackground(getResources().getDrawable(R.drawable.dialog_button_drawable));
            }
        }
    });
    dialog.show();

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/custom_button_background" android:state_focused="true" android:state_pressed="false" />
    <item android:drawable="@android:drawable/btn_default" android:state_focused="true" android:state_pressed="true" />
    <item android:drawable="@android:drawable/btn_default" android:state_focused="false" android:state_pressed="true" />
    <item android:drawable="@android:drawable/btn_default" />
</selector>

2 个答案:

答案 0 :(得分:0)

很抱歉,如果我没有正确地提出你的问题,但是既然你正在装载抽屉,那么这不适合你吗?

How to change colors of a Drawable in Android?

答案 1 :(得分:0)

创建AlertDialog时,您可以设置要使用的主题。

示例 - 创建对话框

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
    builder.setTitle("AppCompatDialog");
    builder.setMessage("");
    builder.setPositiveButton("OK", null);
    builder.setNegativeButton("Cancel", null);
    builder.show();

styles.xml - 自定义样式

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <!-- Used for the buttons -->
    <item name="colorAccent">#FFC107</item>
    <!-- Used for the title and text -->
    <item name="android:textColorPrimary">#FFFFFF</item>
    <!-- Used for the background -->
    <item name="android:background">#4CAF50</item>
</style>

要更改标题的外观,您可以执行以下操作。首先添加一种新风格:

<style name="MyTitleTextStyle">
    <item name="android:textColor">#FFEB3B</item>
    <item name="android:textAppearance">@style/TextAppearance.AppCompat.Title</item>
</style>

之后只需在MyAlertDialogStyle

中引用此款式
<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    ...
    <item name="android:windowTitleStyle">@style/MyTitleTextStyle</item>
</style>

通过这种方式,您可以通过android:textColorPrimary为消息定义不同的textColor,并通过样式为标题定义不同的文本。