Android警告对话框 - 如何更改标题颜色和文本颜色而不更改主要颜色

4

嗨,

是否有一种简单的方法可以在不改变整个应用程序主要颜色的情况下更改标题和文本颜色?

这是我现在得到的:

enter image description here

我不想改变textColorPrimary


请查看以下链接:https://dev59.com/rlkT5IYBdhLWcg3wbO8g - John Joe
4个回答

7
首先,创建一个像这样的样式定义:
<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,但它不会以影响应用程序中其他任何内容的方式进行。

那并没有改变任何东西。此外,我还必须做其他事情来隐藏盒子阴影效果,例如bottomBrighttopDarktopBright - Raheel Hasan
2
@RaheelHasan 你是在导入 android.app.AlertDialog 还是支持库版本的 android.support.v7.app.AlertDialog - Ben P.
是的,这对我也没有用! - AtomicallyBeyond

1

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

Html.fromHtml("<font color='#FFFFFF'>TITLE</font>")

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


1
简单易实现:
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();

用相同的方式处理消息。


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();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接