默认新建的 DialogFragment 是有边距的,想要充满整个屏幕,需要在onStart()里面加入下面代码:
@Override
public void onStart() {
super.onStart();
WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = UIUtils.getScreenWidth(getContext());
params.height = RelativeLayout.LayoutParams.MATCH_PARENT;
params.gravity = Gravity.BOTTOM;
//高度自己定义
getDialog().getWindow().setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, DensityUtil.dip2px(492));
}
PS:而且记得在onCreat()里面设置dialog样式 setStyle(),要不然不起作用
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.fragment_dialog);
}
fragment_dialog样式
<style name="fragment_dialog" parent="@style/Theme.AppCompat.Dialog">
<item name="android:windowAnimationStyle">@style/ShareDialogAnimation</item>
<item name="android:windowCloseOnTouchOutside">true</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
网友评论