android - 更改android对话框的背景颜色

标签 android background dialog

我有一个用 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 文件,在此处:

https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/layout/alert_dialog_micro.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/

相关文章:

安卓应用升级

android - 是否有适用于 Android FAX 的 native 应用程序?

android - 在 NumberPicker 中显示特定值

jsf - 从 PrimeFaces Dialog Framework 的对话框更新父窗口中的组件

java - ConcurrentLinkedQueue 未按预期工作

java - Activity 不可分配给 Activity

html - 背景顶部的种类(如边框顶部)

c# - 仅在后台操作较长时显示进度

android - 使背景覆盖所有屏幕尺寸

android - 当我在自定义对话框外单击时,自定义对话框被关闭