How to change the background color around a DialogFragment?

前端 未结 8 1861
时光取名叫无心
时光取名叫无心 2020-11-29 01:59

I\'m building a custom DialogFragment. The dialog layout is set to my_dialog.xml, but how can I modify the color around the dialog (the transparent

8条回答
  •  孤街浪徒
    2020-11-29 02:22

    I wanted a transparent background for my DialogFragment, and, in code, this works fine:

    @Override
    public void onStart() {
        super.onStart();
    
        Window window = getDialog().getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
    }
    

    Of course, you can specify any color or Drawable using setBackgroundDrawable() or setBackgroundDrawableResource().

    This works at least in onStart(), but not in onCreate(), and not necessarily in onCreateView(), it seems.

    That said, in most cases it's probably cleaner to do this in XML, using styles, along these lines:

    
    

提交回复
热议问题