我有一个用 AlertDialog.Builder 构建的对话框,我想更改对话框的背景颜色。
我一直在互联网上读到可以使用 ContextThemeWrapper(使用 API 10),但它不起作用。
我正在做的是:
ContextThemeWrapper wrapper = new ContextThemeWrapper(this, R.style.MyDialogTheme);
AlertDialog alertDialog = new AlertDialog.Builder(wrapper)).create();
<style name="MyDialogTheme" parent="@android:style/Theme.Dialog">
<item name="android:background">#FFFFFF</item>
</style>
为什么这不起作用?
提前致谢!
最佳答案
alertDialog xml 文件,在此处:
只需更改它们的值:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(_title);
builder.setMessage(_message);
//....
AlertDialog dialog = builder.show();
int objeto = getResources().getIdentifier("buttonPanel","id","android");
View vistaObjeto=dialog.findViewById(objeto);
if (vistaObjeto!=null){
vistaObjeto.setBackgroundColor(Color.RED);
}
objeto = getResources().getIdentifier("topPanel", "id", "android");
vistaObjeto = dialog.findViewById(objeto);
if (vistaObjeto != null){
vistaObjeto.setBackgroundColor(Color.YELLOW);
}
objeto = getResources().getIdentifier("alertTitle","id","android");
vistaObjeto=dialog.findViewById(objeto);
if (vistaObjeto!=null){
((TextView)vistaObjeto).setTextColor(Color.BLACK);
}
objeto = getResources().getIdentifier("titleDivider","id","android");
vistaObjeto=dialog.findViewById(objeto);
if (vistaObjeto!=null){
vistaObjeto.setBackgroundColor(Color.GREEN);
}
objeto = getResources().getIdentifier("contentPanel","id","android");
vistaObjeto=dialog.findViewById(objeto);
if (vistaObjeto!=null){
vistaObjeto.setBackgroundColor(Color.BLUE);
}
objeto = getResources().getIdentifier("buttonPanel","id","android");
vistaObjeto=dialog.findViewById(objeto);
if (vistaObjeto!=null){
vistaObjeto.setBackgroundColor(Color.MAGENTA);
}
objeto = getResources().getIdentifier("parentPanel","id","android");
vistaObjeto=dialog.findViewById(objeto);
if (vistaObjeto!=null){
vistaObjeto.setBackgroundColor(Color.CYAN);
}
return dialog;
}
关于android - 更改android对话框的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12973265/