默认DialogFragment距离屏幕的两边有一定的间距,所以宽度填充满需要手动设置。
1, 在start设置它的宽度
override fun onStart() {
super.onStart()
val dialog = dialog
dialog!!.setCancelable(false)
dialog.setCanceledOnTouchOutside(false)
if (dialog != null) {
//java
//DisplayMetrics dm =new DisplayMetrics();
val dm = DisplayMetrics()
activity!!.windowManager.defaultDisplay.getMetrics(dm)
dialog.window!!.setLayout(dm.widthPixels * 1, ViewGroup.LayoutParams.WRAP_CONTENT)
//dialog.window!!.setLayout( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
}
}
2,必须注意,单这么设置还不行,必须设置它的style,否则不起作用。
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialog);
}
style name="MyDialog" parent="@style/Theme.AppCompat.Dialog">
<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/white</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
//TODO 设置dialog位置
private void initData(View view) {
Dialog dialog = getDialog();
Window window = dialog.getWindow();
window.setGravity(Gravity.CENTER);
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
网友评论