Android 去掉 DialogFragment 浮动边距

Android get rid of DialogFragment floating margin

我在我的应用程序中显示一个对话框片段,我注意到即使我将片段的 y 位置设置到屏幕底部,仍然有可见的边距:

        Display display = getActivity().getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        windowParams.y = size.y;

在下面的屏幕截图中,您可以看到尽管设置为屏幕底部,但淡蓝色(我的 dialogfragment)仍然出现在离屏幕底部有一段距离的地方。如何删除此边距?

解决方法是添加这一行:

setStyle(DialogFragment.STYLE_NO_FRAME, 0);

为避免在底部、顶部、左侧和右侧使用额外的边距,请使用以下代码行。

//Dialog fragment will be shown at the bottom of screen 

//if you want to show on entire screen just comment it

getDialog().getWindow().setGravity(Gravity.BOTTOM);

WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = getDialog().getWindow();



lp.copyFrom(window.getAttributes());

 //This makes the dialog take up the full width



lp.width = WindowManager.LayoutParams.MATCH_PARENT;

//For full height set it to MATCH_PARENT else WRAP_CONTENT



lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

window.setAttributes(lp);