如何更改安卓警告对话框标题分隔线颜色

11
我想为警告对话框中的标题样式或更改分隔符颜色。我搜索了这个问题,我认为很多人都在寻找这个答案。但我仍然无法找到正确的解决方案。我想更改以下内容。 蓝色标题分隔符

你尝试使用setDivider方法了吗?它在警告对话框中存在吗? - Paschalis
我为我的旧版Android创建了自定义对话框,但是对于新版Android,我想更改这个蓝色分隔符的颜色。对于旧版本,我完全创建了自定义对话框。但是我不想在较高版本中使用它。所以这是个问题。有没有办法改变这种情况呢? - nilkash
如果您不想创建自定义对话框,则可能需要更改主题,否则您必须使用自定义对话框。 - Janmejoy
我已经尝试将其应用于它们,以便更改标题的颜色。但是我无法更改分隔符的颜色。而且我真的不想在我的较高版本的Android上使用自定义警报对话框。 - nilkash
你找到解决方案了吗? - Mario Zderic
1
这可能会对你有所帮助:- https://dev59.com/B2Yq5IYBdhLWcg3wfgvd - pRaNaY
5个回答

16

你可以通过一个非常简单的技巧实际上改变AlertDialog标题的颜色:

public static void brandAlertDialog(AlertDialog dialog) {
    try {
        Resources resources = dialog.getContext().getResources();
        int color = resources.getColor(...); // your color here

        int alertTitleId = resources.getIdentifier("alertTitle", "id", "android");
        TextView alertTitle = (TextView) dialog.getWindow().getDecorView().findViewById(alertTitleId);
        alertTitle.setTextColor(color); // change title text color

        int titleDividerId = resources.getIdentifier("titleDivider", "id", "android");
        View titleDivider = dialog.getWindow().getDecorView().findViewById(titleDividerId);
        titleDivider.setBackgroundColor(color); // change divider color
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

2
这个“future”安全吗? - loeschg
最安全的我知道 - MatrixDev
@Patrick,请问一下它在哪些Android版本上无法工作,例如在15以下或17以上等,这样可以帮助其他人避免检查不同的版本。 - Shirish Herwade

4

分隔线颜色:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.dialog)
   .setIcon(R.drawable.ic)
   .setMessage(R.string.dialog_msg);
Dialog d = builder.show();
int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
View divider = d.findViewById(dividerId);
divider.setBackgroundColor(getResources().getColor(R.color.my_color));

1
关键在于先运行.show(),然后访问对话框以获取分隔符视图并进行更新等操作。我一开始错过了这个步骤 - 尝试在显示对话框之前更新颜色,但没有起作用。非常简洁的示例 - 谢谢。 - Gene Bo

0

我已经找到了更新alertBuilder对话框分隔符颜色的最简单解决方案。

没有直接的方法可以实现更改警报对话框分隔符的颜色。因此,我们可以使用默认分隔符的ID来绕过警报对话框的对象。

通常,我们会在alertBuilders属性和函数的末尾添加alert.show()。而不是alert.show(),您需要替换下面的一组行以更新警报对话框的分隔符颜色。

 AlertDialog.Builder alert = new AlertDialog.Builder(SettingActivity.this);
            alert.setTitle("Delete");
            alert.setMessage("Are you sure want to delete ");
            alert.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                   //Delete operations
                }
            });
            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            alert.setIcon(android.R.drawable.ic_dialog_alert);
            Dialog d = alert.show();
            int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
            View divider = d.findViewById(dividerId);
            divider.setBackgroundColor(getResources().getColor(R.color.blue_color));

        }
    });

重要提示:请勿在代码底部使用任何其他的alert.show()。如上所述,alert将自动从第二行获取show属性。


-1
根据源代码,这个颜色似乎是硬编码的。我认为这是一个错误,应该可以进行样式化。
不过有一个简单的解决方法:使用 setStyle(DialogFragment.STYLE_NO_TITLE, R.style.myStyle);,并编写一个简单的线性布局,其中第一项是您的标题。

2
你应用setStyle到哪里了?在AlertDialogs中没有这样的方法(我所看到的)。如果这有效,我很乐意给你点赞。 - PeteH
1
我在DialogFragment的onCreate方法中使用它(我建议您使用DialogFragment而不是Fragment:https://dev59.com/questions/y2Yr5IYBdhLWcg3wVYsg) - Teovald

-3
QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(context).
        setTitle("Set IP Address").
        setTitleColor("#FF00FF").
        setDividerColor("#FF00FF").
        setMessage("You are now entering the 10th dimension.").
qustomDialogBuilder.show();

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