我想为警告对话框中的标题样式或更改分隔符颜色。我搜索了这个问题,我认为很多人都在寻找这个答案。但我仍然无法找到正确的解决方案。我想更改以下内容。
![蓝色标题分隔符](https://istack.dev59.com/jNqo8.webp)
![蓝色标题分隔符](https://istack.dev59.com/jNqo8.webp)
你可以通过一个非常简单的技巧实际上改变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));
.show()
,然后访问对话框以获取分隔符视图并进行更新等操作。我一开始错过了这个步骤 - 尝试在显示对话框之前更新颜色,但没有起作用。非常简洁的示例 - 谢谢。 - Gene Bo我已经找到了更新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属性。
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.myStyle);
,并编写一个简单的线性布局,其中第一项是您的标题。QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(context).
setTitle("Set IP Address").
setTitleColor("#FF00FF").
setDividerColor("#FF00FF").
setMessage("You are now entering the 10th dimension.").
qustomDialogBuilder.show();