要修改Android对话框背景颜色,在正按钮后面不改变,可以使用自定义对话框样式来实现。
首先,在res/values/styles.xml文件中定义一个自定义对话框样式:
然后,在res/values/colors.xml文件中定义自定义对话框的背景颜色:
#FF0000
接下来,在代码中使用自定义对话框样式创建对话框:
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.CustomDialog));
builder.setTitle("Title");
builder.setMessage("Message");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// 点击OK按钮的逻辑处理
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// 点击Cancel按钮的逻辑处理
}
});
AlertDialog dialog = builder.create();
dialog.show();
上述代码中,通过ContextThemeWrapper
将自定义对话框样式应用到对话框中。
最后,在自定义对话框样式中使用android:windowBackground
和android:colorBackground
来设置对话框的背景颜色。
这样,就可以实现对话框背景颜色在正按钮后面不改变的效果了。
下一篇:Android:对话框标题不可见