Android警报对话框 - 如何在不更改主要颜色的情况下更改标题颜色和文本颜色

时间:2018-02-27 02:30:07

标签: android dialog alert android-alertdialog android-styles

干草,

是否有简单方式更改titletext颜色而不更改整个应用的主要颜色?

这是我现在所拥有的:

enter image description here

我不想更改textColorPrimary

4 个答案:

答案 0 :(得分:3)

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

<style name="DialogTheme" parent="ThemeOverlay.AppCompat.Dialog">
    <item name="android:textColorPrimary">your color here</item>
</style>

然后,当您创建对话框而不是使用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();

enter image description here

即使这取决于更改textColorPrimary,但它不会影响您应用中的任何其他内容。

答案 1 :(得分:0)

试试这个,

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(Html.fromHtml("<font color='#0288D1'>This is a test</font>"));
builder.setPositiveButton(Html.fromHtml("<font color='#0288D1'>Yes</font>"), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int arg1) {
        Log.e(LOG_TAG, "Yes");
    }
});
builder.setNegativeButton(Html.fromHtml("<font color='#0288D1'>No</font>"), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int arg1) {
        Log.e(LOG_TAG, "No");
    }
});
builder.create();
builder.show();

答案 2 :(得分:0)

简单易行:

SpannableString title = new SpannableString("Your title");
                title.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.your_color)), 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

然后在

中传递标题
new AlertDialogBuilder(context).setTitle(title).show();

对消息做同样的事。

答案 3 :(得分:0)

简单来说,您只需使用以下代码更改“ TITLE”文本:

import datetime dateInput= '10/11/2018' dateFormat = '%d/%m/%Y' dateObj= datetime.datetime.strptime(dateInput, dateFormat )

此代码将完全根据您添加的颜色值更改文本颜色。您也可以将其用于按钮的文本。

相关问题