我有一个主要 Activity ,用户可以(通过首选项)将背景颜色更改为他们喜欢的颜色。我的问题是我无法更改任何自定义对话框的背景颜色。
堆栈溢出中的其他答案建议:
(a) overiding the default theme到首选颜色。在这种情况下,我认为这不是一个合适的解决方案,因为我知道不建议在运行时更改主题。
(b) Changing in styles.xml (在这种情况下不适合,因为我无法在运行时更改)
(c) Overriding the AlertBuilder class (但这会影响整个警报对话框)
我最接近更改颜色的方法是删除警报生成器标题,并将自定义 View 的背景设置为最喜欢的颜色(例如粉红色)。不幸的是,这在对话框的顶部和底部给出了一个丑陋的 strip 。
图像后包含代码,非常感谢有关如何更改对话框背景的建议。
默认外观代码
protected Dialog onCreateDialog(int dialogId) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final View viewMessEdit = inflater.inflate(R.layout.example,(ViewGroup) findViewById(R.id.dialog_mess_edit_root));
builder.setView(viewMessEdit);
builder.setTitle("Alert builder's title");
}
更改自定义对话框 View 背景颜色的代码(并且删除了警报构建器的标题)
protected Dialog onCreateDialog(int dialogId) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final View viewMessEdit = inflater.inflate(R.layout.example,(ViewGroup) findViewById(R.id.dialog_mess_edit_root));
builder.setView(viewMessEdit);
viewMessEdit.setBackgroundResource(R.color.pink_dark);
}
最佳答案
我找到了一个单一规则的解决方案!
d.getWindow().setBackgroundDrawableResource(R.drawable.menubackground);
它适用于我的普通对话框。
但我不知道它是否适用于 AlertDialog
。
关于android - 如何以编程方式更改对话框背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7269706/