DialogFragment with clear background (not dimmed)

六月ゝ 毕业季﹏ 提交于 2019-11-27 07:00:23

What works for me is to adjust the WinowManager.LayoutParams in onStart() of the DialogFragment:

@Override public void onStart() {
    super.onStart();

    Window window = getDialog().getWindow();
    WindowManager.LayoutParams windowParams = window.getAttributes();
    windowParams.dimAmount = 0.90f;
    windowParams.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    window.setAttributes(windowParams);
}
samis

You need to get a handle to your DialogFrament (sometime after .show is called), and do this in a Posted Runnable:

DialogFragment dialog;

...

WindowManagerLayoutParams wlp = dialog.Dialog.Window.Attributes;
wlp.Flags &= ~WindowManagerFlags.DimBehind;
dialog.Dialog.Window.Attributes = wlp;

I got it from Aleks G's answer to Changing position of the Dialog on screen android .

Even a simpler solution is to change the style in the onCreate of the DialogFragment:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NO_FRAME, getTheme());
}

Create your own Customized dialog extends with FragmentDailog and override this method

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);

    //set the dialog to non-modal and disable dim out fragment behind
    Window window = dialog.getWindow();
    window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    return dialog;
}

NOTE: This answer works for me in case of DialogFragment and BottomSheetDialogFragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!