@Override
public void onStart() {
super.onStart();
Window window = getDialog().getWindow();
if (window != null) {
//设置 window 的背景色为透明色.
//如果通过 window 设置宽高时,想要设置宽为屏宽,就必须调用下面这行代码。
window.setBackgroundDrawable(new ColorDrawable(0xff000000));
WindowManager.LayoutParams attributes = window.getAttributes();
//在这里我们可以设置 DialogFragment 弹窗的位置
attributes.gravity = Gravity.BOTTOM;
//我们可以在这里指定 window的宽高
Display display = getActivity().getWindowManager().getDefaultDisplay();
attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
// 设置窗体的高度
// lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
attributes.height = Utils.getScreenHeight(mContext)*3/4;
window.getDecorView().setPadding(0, 0, 0, 0);
//设置 DialogFragment 的进出动画
// attributes.windowAnimations = R.style.DialogAnimation;
window.setAttributes(attributes);
}
}
————————————————
版权声明:本文为CSDN博主「qq3290510686」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36388797/article/details/107211032
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Dialog dialog;
//设置位置
dialog = super.onCreateDialog(savedInstanceState);
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM);
return dialog;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSourceType = MediaConstant.NONE_MUSIC;
Bundle args = getArguments();
if (args != null) {
mSourceType = args.getInt(MediaConstant.EXTRAS_SOURCE_TYPE);
}
MLog.d(TAG, "mSourceType: " + mSourceType);
}
@Nullable
@Override
public View onCreateView(
LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
//设置背景
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(getActivity()
.getDrawable(R.drawable.bg_main_color));
return inflater.inflate(R.layout.fragment_no_header_list, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initView(view);
bindViewModel();
}
@Override
public void onStart() {
super.onStart();
//设置宽高
int width = ScreenUtil.dp2px(getActivity(), 1440f);
int height = ScreenUtil.dp2px(getActivity(), 824f);
getDialog().getWindow().setLayout(width, height);
}
开启Fragment事务,将DialogFragment加到回退栈中show()
方法进行显示
private fun showDialog(str: String, item: Equipment) {
val ft: FragmentTransaction = parentFragmentManager.beginTransaction()
val prev: Fragment? = getFragmentManager()?.findFragmentByTag("dialog")
if (prev != null) {
ft.remove(prev)
}
ft.addToBackStack(null)
val newFragment: mDialog= mDialog.newInstance(item)
newFragment.show(ft, "dialog")
}
}