繁体   English   中英

如何更改警报对话框 header 分隔线颜色 android

[英]How to change alert dialog header divider color android

我想在警报对话框中设置或更改 header 标题的分隔线颜色。 我搜索了这个问题,我想很多人都在搜索这个。但我仍然无法找到正确的解决方案。 我想更改以下内容。蓝色标头分隔线

你可以通过一个非常简单的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();
    }
}

分频器颜色: -

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

从来源来看,似乎这种颜色是硬编码的。 我认为这是一个错误,它应该是可定制的imho。

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

我找到了将 colors 更新为 alertBuilder 的对话框分隔线的最简单解决方案。

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

通常,我们会在 alertbuilders 属性和 function 的末尾添加 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() 。 如上所述,警报将自动从第二行获取显示属性。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM