您的位置:首页 > 其它

自定义FragmentDialog视图大小和位置设置

2017-09-04 23:12 741 查看

今天在写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)

去掉默认标题.

即达到了效果.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  自定义dialog