【问题标题】:Android Alert Dialog - How to change the title color and the text color without changing the primary colorAndroid Alert Dialog - 如何在不更改原色的情况下更改标题颜色和文本颜色
【发布时间】:2018-02-27 02:30:06
【问题描述】:

干草,

有没有一种简单的方法来改变titletext的颜色而不改变整个应用的原色?

这是我现在得到的:

我不想更改textColorPrimary

【问题讨论】:

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


【解决方案1】:

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

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

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

【讨论】:

  • 这并没有改变任何东西..另外我不得不做其他事情来隐藏盒子阴影效果,例如bottomBrighttopDarktopBright
  • @RaheelHasan 你是在导入android.app.AlertDialog 还是支持库版本android.support.v7.app.AlertDialog
  • 是的,这对我也不起作用!
【解决方案2】:

简单易实现:

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】:

    试试这个,

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

    【讨论】:

      【解决方案4】:

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

      Html.fromHtml("&lt;font color='#FFFFFF'&gt;TITLE&lt;/font&gt;")

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-24
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多