Dialogfragment 使用记录

2,449 阅读1分钟

android 4.4 dialog显示异常,dialog上方会出现一条横杠,以下是去掉横杠的代码


@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    if (getDialog().getWindow() != null) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    }
    return  inflater.inflate(R.layout.dialog_bind, container, false);
}

固定宽度设置的两种实现

  • 布局中子view 需要固定大小
  • 在代码中限制window的大小
@Override
public void onStart() {
    super.onStart();
    Dialog dialog = getDialog();
    DisplayMetrics dm =new DisplayMetrics();
    if (getActivity() != null && dialog != null && dialog.getWindow() != null) {
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        dialog.getWindow().setLayout((int) (dm.widthPixels * 0.75),ViewGroup.LayoutParams.WRAP_CONTENT);
    }

}

背景透明, 去掉背景蒙层,点击外部区域是否cancel

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
	  if (getDialog().getWindow() != null) {
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            getDialog().getWindow().setDimAmount(0f);
            getDialog().setCanceledOnTouchOutside(false);
        }
	return inflater.inflate(R.layout.dialog_login, container, false);
}