Android Alert Dialog - 如何在不改变原色的情况下更改标题颜色和文本颜色

Rah*_*san 4 alert android dialog android-alertdialog android-styles

干草,

有没有一种简单的方法可以在不改变整个应用程序titletext原色的情况下更改 和 颜色?

这是我现在得到的:

在此处输入图片说明

我不想改变 textColorPrimary

Ben*_* P. 6

首先,创建一个这样的样式定义:

<style name="DialogTheme" parent="ThemeOverlay.AppCompat.Dialog">
    <item name="android:textColorPrimary">your color here</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后,当您创建对话框时,不要使用AlertDialog.Builder(Context)构造函数,而是使用该AlertDialog.Builder(Context, int)方法并传递对您的样式的引用:

new AlertDialog.Builder(this, R.style.DialogTheme)
        .setTitle("Hello world")
        .setMessage("some longer text for the body of the dialog")
        .show();
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

尽管这取决于更改textColorPrimary,但它不会以影响您应用程序中其他任何内容的方式这样做。

  • @RaheelHasan 您是导入`android.app.AlertDialog` 还是支持库版本`android.support.v7.app.AlertDialog`? (2认同)