限制DialogFragment相对屏幕的最大高度
@Override
public void onResume() {
super.onResume();
initDialogParams();
}
private void initDialogParams() {
Window window = getDialog().getWindow();
WindowManager windowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
if (window != null && windowManager != null) {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED);
WindowManager.LayoutParams lp = window.getAttributes();
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Display display = windowManager.getDefaultDisplay();
//调节灰色背景透明度[0-1],默认0.5f
lp.dimAmount = 0.5f;
//是否在底部显示
lp.gravity = Gravity.BOTTOM;
//设置dialog宽度
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
//设置dialog高度
Point pSize = new Point();
display.getSize(pSize);
lp.height = (int) (pSize.y * 0.75);
window.setAttributes(lp);
}
setCancelable(false);//设置点击外部是否消失
}