更改android对话框的背景颜色

时间:2012-10-19 11:26:27

标签: android background dialog

我有一个用AlertDialog.Builder构建的Dialog,我想更改对话框的背景颜色。

我一直在互联网上阅读,可以使用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>

为什么这不起作用?

提前致谢!

3 个答案:

答案 0 :(得分:2)

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;
}

答案 1 :(得分:0)

将以下代码用于自定义对话框:

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);
}

请点击此链接:

答案 2 :(得分:0)

我已使用AlertDialog.THEME_DEVICE_DEFAULT_LIGHT修改了我的AlertDialog,因此我编写了我的构建器:

AlertDialog.Builder dialog = new AlertDialog.Builder(ApplicationCertificatesListFragment.this.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);