How to change dialog background color programmatically?

前端 未结 5 1824
栀梦
栀梦 2020-12-08 16:30

I have a main activity where users can change (via preferences) the background colour to their favourite colour. My problem is that I can not change the background colour o

5条回答
  •  有刺的猬
    2020-12-08 16:46

    The suggested solution

    d.getWindow().setBackgroundDrawableResource(R.drawable.menubackground);
    

    Do not work well (tested in 6.0) as it changes the dialog shadow

    Here Is my solution:

     public static void changeDialogBackground(final Dialog dialog,final int resId) {
        dialog.getWindow().getDecorView().setBackgroundResource(resId);
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(final DialogInterface dialog) {
                Window window = ((AlertDialog)dialog).getWindow();
                window.getDecorView().setBackgroundResource(resId);
            }
        });
    }
    

提交回复
热议问题