今天在写FragmentDialog的时候,想让布局随着逻辑变化,发现自定义FragmentDialog的窗口大小和位置都不是自己想要的,查完资料后解决了.
设置的方法如下两步
- 在DialogFragment的onStart方法中添加
Dialog dialog = getDialog()
if (dialog != null) {
DisplayMetrics dm = new DisplayMetrics()
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm)
WindowManager.LayoutParams attributes = dialog.getWindow().getAttributes()
attributes.gravity = Gravity.TOP
attributes.y = (int) DisplayUtil.dp2Px(getContext(), 100)
dialog.getWindow().setAttributes(attributes)
dialog.getWindow().setLayout((int) (dm.widthPixels * 0.9), ViewGroup.LayoutParams.WRAP_CONTENT)
}
- 在onCreate里添加setStyle(DialogFragment.STYLE_NO_TITLE, 0)
去掉默认标题.
即达到了效果.