如何更改AlertDialog中的文本颜色

2023-11-27

AlertDialog

如何更改 AlertDialog 中的文本颜色?

<item name="android:textColor">@color/black_text</item>

这仅更改标题颜色。

ad = new AlertDialog.Builder((new ContextThemeWrapper(context, R.style.DialogTheme)));
            ad.setTitle(R.string.my_activ_remove_title_dialog);

            ad.setPositiveButton(R.string.my_activ_remove_dialog, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int arg1) {
                    content.remove(position);
                    notifyItemRemoved(position);
                    notifyItemRangeChanged(position, content.size());
                }

            });
            ad.setNegativeButton(R.string.my_activ_cancel_remove_dialog, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int arg1) {

                }
            });

警报对话框 v2


如果仅更改字体颜色,请尝试以下操作:

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

result:

enter image description here


要更改字体颜色和按钮背景颜色,请尝试以下操作:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(Html.fromHtml("<font color='#FF7F27'>This is a test</font>"));
    builder.setCancelable(false);
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
    Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
    //Set negative button background
    nbutton.setBackgroundColor(Color.MAGENTA);
    //Set negative button text color
    nbutton.setTextColor(Color.YELLOW);
    Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
    //Set positive button background
    pbutton.setBackgroundColor(Color.YELLOW);
    //Set positive button text color
    pbutton.setTextColor(Color.MAGENTA);

Result:

Result:


如果您想更改分隔线颜色,请尝试以下操作:

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Test Title");
        builder.setMessage(Html.fromHtml("<font color='#FF7F27'>This is a test</font>"));
        builder.setCancelable(false);
        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
        try {
            Resources resources = dialog.getContext().getResources();
            int alertTitleId = resources.getIdentifier("alertTitle", "id", "android");
            TextView alertTitle = (TextView) dialog.getWindow().getDecorView().findViewById(alertTitleId);
            alertTitle.setTextColor(Color.MAGENTA); // change title text color

            int titleDividerId = resources.getIdentifier("titleDivider", "id", "android");
            View titleDivider = dialog.getWindow().getDecorView().findViewById(titleDividerId);
            titleDivider.setBackgroundColor(Color.YELLOW); // change divider color
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        Button nbutton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
        //Set negative button background
        nbutton.setBackgroundColor(Color.MAGENTA);
        //Set negative button text color
        nbutton.setTextColor(Color.YELLOW);
        Button pbutton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
        //Set positive button background
        pbutton.setBackgroundColor(Color.YELLOW);
        //Set positive button text color
        pbutton.setTextColor(Color.MAGENTA);

这是我的示例代码,但如果您想更改分隔线颜色,请考虑以“int titleDividerId”开头的代码部分。

Result:

This is the result of the code


如果你想大量定制AlertDialog。例如,添加一些具有自定义背景颜色的复选框,请使用以下方法:

AlertDialog.Builder alert = new AlertDialog.Builder(this);
        LinearLayout mainLayout       = new LinearLayout(this);
        mainLayout.setOrientation(LinearLayout.VERTICAL);

        LinearLayout layout1       = new LinearLayout(this);
        layout1.setOrientation(LinearLayout.HORIZONTAL);
        CheckBox cb1 = new CheckBox(getApplicationContext());
        cb1.setText("Easy");
        layout1.addView(cb1);
        layout1.setBackgroundColor(Color.BLUE);
        layout1.setMinimumHeight(50);

        LinearLayout layout2       = new LinearLayout(this);
        layout2.setOrientation(LinearLayout.HORIZONTAL);
        layout2.addView(new TextView(this));
        CheckBox cb2 = new CheckBox(getApplicationContext());
        cb2.setText("Normal");
        layout2.addView(cb2);
        layout2.setBackgroundColor(Color.CYAN);
        layout2.setMinimumHeight(50);

        LinearLayout layout3       = new LinearLayout(this);
        layout3.setOrientation(LinearLayout.HORIZONTAL);
        CheckBox cb3 = new CheckBox(getApplicationContext());
        cb3.setText("Hard");
        layout3.addView(cb3);
        layout3.setBackgroundColor(Color.GREEN);
        layout3.setMinimumHeight(50);

        mainLayout.addView(layout1);
        mainLayout.addView(layout2);
        mainLayout.addView(layout3);
        alert.setTitle("Custom alert demo");
        alert.setView(mainLayout);
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        alert.setPositiveButton("Done", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getBaseContext(), "done", Toast.LENGTH_SHORT).show();
            }
        });

        alert.show();

结果:

The result

首先,我创建了一个主布局(垂直),如您在代码中看到的。然后,我为每个复选框创建了一个水平布局。在这种情况下,您可以使用元素(复选框、项目等)的颜色和字体。我希望它有帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何更改AlertDialog中的文本颜色 的相关文章

随机推荐

  • 从 http 基本身份验证中排除特定的 cakephp 控制器

    我试图排除路径 URI 被基本 http 身份验证阻止 路径是 rest http example com rest 并代表 cakephp 3 应用程序的控制器 它不是一个真实的文件 而是一个由重写条件重写并由 webroot 目录中的
  • r sf包多边形内的质心

    我需要向多边形添加标签 并且通常使用质心 但是质心不会落在多边形内 我发现这个问题计算 SpatialPolygon 内 内部的质心但我正在使用 sf 包 下面是玩具数据 rm list ls all TRUE start with emp
  • 绘制矩形和Interface Builder之间的颜色差异?

    简而言之 我在界面生成器中有 2 个视图 其中一个使用界面生成器中的 RGB 滑块设置为颜色 99 99 99 另一个视图以编程方式着色以实现某种形状 我使用以下方式填充它 Obviously this is in drawRect UIC
  • 如何修改非常大的 zip 中的单个文件而不重写整个 zip?

    我有包含巨大文件的大型 zip 文件 zip 存档中有一些需要修改的 元数据 文本文件 但是 无法提取整个 zip 并重新压缩它 我需要在 zip 中找到目标文本文件 对其进行编辑 并可能将更改附加到 zip 文件中 文本文件的文件名始终相
  • 从 Option>> 解开并访问 T

    我正在尝试用 Rust 解决一些 Leetcode 问题 然而 我在使用 LeetCode 时遇到了一些困难TreeNode执行 use std cell RefCell use std rc Rc TreeNode data struct
  • Android:WebView的方法goBack()显示空白页面

    我有一个 Android 应用程序 它使用 WebView 在活动中加载网页 我正在使用手动检索页面并使用 WebView 的使用BaseURL加载数据将其显示在屏幕上 那里的一切都很好 现在 我尝试覆盖 后退 按钮按下以模拟在 WebVi
  • 如何将 iOS React Native 模板转换为 Swift?

    我一直在阅读有关 React Native 的内容 并决定尝试一下 我在 Swift 方面很有经验 但从未尝试过 Objective C 所以我想将模板项目转换为使用 AppDelegate swift 我按照这个接受的答案中的解决方案进行
  • Fetch API 与 XMLHttpRequest

    我知道 Fetch API 使用Promises 并且它们都允许您向服务器发出 AJAX 请求 我读到 Fetch API 有一些额外的功能 这些功能在XMLHttpRequest 以及在 Fetch API polyfill 中 因为它基
  • 什么时候有人会使用工会?这是纯 C 时代的残余吗?

    我学到了 但并没有真正加入工会 我读过的每本 C 或 C 文本都会介绍它们 有时是顺便介绍 但它们往往很少给出关于为什么或在哪里使用它们的实际示例 工会何时在现代 甚至遗留 情况下有用 我唯一的两个猜测是 当您的工作空间非常有限时 或者当您
  • 如何将compile_commands.json与clang python绑定一起使用?

    我有以下脚本尝试打印给定 C 文件中的所有 AST 节点 当在具有简单包含的简单文件 同一目录中的头文件等 上使用它时 效果很好 usr bin env python from argparse import ArgumentParser
  • 如何在生产中禁用转储 symfony 功能

    如何禁用dump 功能 什么时候在生产环境中 如果我忘记了转储功能 它会崩溃并出现 500 错误 你应该删除dump 来自您的生产代码 它不必在那里 But as noted by Cerad 因为当您在签入之前忘记删除它们时可能会很烦人
  • 我如何告诉 PyCharm 参数的预期类型是什么?

    当涉及到构造函数 赋值和方法调用时 PyCharm IDE 非常擅长分析我的源代码并找出每个变量应该是什么类型 我喜欢它正确的时候 因为它为我提供了良好的代码完成和参数信息 并且如果我尝试访问不存在的属性 它会给我警告 但当涉及到参数时 它
  • 映射缩减组合器

    我有一个带有映射器 减速器和组合器的简单映射缩减代码 映射器的输出被传递到组合器 但是对于reducer来说 传递的不是combiner的输出 而是mapper的输出 请帮忙 Code package Combiner import jav
  • 如何在 React Native 中限制 google 登录到我公司的电子邮件域 (@company.com)?

    Question 阻止用户使用不以 mycompany com 结尾的电子邮件地址通过 Firebase Google 身份验证登录我们的内部应用程序的最佳方法是什么 Goal 防止用户使用错误的电子邮件登录应用程序 获取用户的公司电子邮件
  • Python pip install 以“命令错误,退出状态 1:...”结束

    我是 python 新手 我正在尝试运行一些需要一些库的基本代码 当我尝试安装库 例如 pip install matplotlib venn 时 我收到这个长错误 ERROR Command errored out with exit s
  • 如何使用 JavaScript 以编程方式打开文件选择器? [复制]

    这个问题在这里已经有答案了 可能的重复 在 JavaScript 中 我可以通过编程方式为文件输入元素触发 click 事件吗 我天真地尝试了以下方法 使用 JavaScript 以编程方式打开文件选择器 请参阅 fiddlehere
  • 每当 firebase 发生任何更改时,自动向所有用户发送通知 [关闭]

    Closed 这个问题需要多问focused 目前不接受答案 当 Firebase 发生更改时 如何通知所有用户 我不想手动执行此操作 您应该使用由实时数据库触发器触发的 Firebase Cloud Function 请参阅文档here
  • 如何启动Manifest中提到的不存在的Activity?

    我正在尝试开发一个 动态 Android 应用程序 动态是指我在清单中列出了一个在运行时 构建 的活动 我可以很好地构建所需的活动 但是 当我尝试启动它时 我的应用程序失败了 java lang RuntimeException Unabl
  • std::sort 会改变相等元素的相对顺序吗?

    标准是否通过使用 std sort 保证相等元素的顺序不会改变 呃 忘记了这个术语 或者我是否需要考虑替代解决方案来实现此目标 std sort不保证稳定 您试图想到的术语 正如你猜想的那样 std stable sort保证稳定 std
  • 如何更改AlertDialog中的文本颜色

    如何更改 AlertDialog 中的文本颜色