如何更改警报对话框标题分隔符颜色android

nil*_*ash 11 android styles dialog colors android-alertdialog

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

Mat*_*Dev 15

你可以通过一个非常简单的hack来改变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();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这个“未来”安全吗? (2认同)
  • 不适用于我测试过的所有Android版本. (2认同)

Teo*_*ald -1

从来源来看,这种颜色似乎是硬编码的。我认为这是一个错误,恕我直言,它应该是可设计的。

不过,有一个简单的解决方法:使用setStyle(DialogFragment.STYLE_NO_TITLE, R.style.myStyle);,并编写一个简单的线性布局,其中第一项是您的标题。

  • 你将 setStyle 应用于什么?AlertDialogs 没有这样的方法(我可以看到)。如果这有效,我很乐意为您的答案投票。 (2认同)