且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何改变警告对话框标题分颜色的android

更新时间:2023-10-03 22:58:58

实际上,你可以通过一个非常简单的黑客更改AlertDialog标题颜色:

You can actually change color of AlertDialog title by a very simple hack:

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