DialogFragment是系統推薦的dialog,至於它的好處有很多,最重要的是通過Fragment可以把dialog納入到Fragment的生命週期中,這樣就不會出現dialog顯示在activity中旋轉屏幕後dialog發生錯誤。
1 自定義dialogFragment
public class MyDialogFragment extends DialogFragment {
private Context mContext;
private View baseView;
public static MyDialogFragment newInstance(int type) {
Bundle args = new Bundle();
args.putInt("type", type);
MyDialogFragment fragment = new MyDialogFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.mContext = context;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final Dialog dialog = getDialog();
if (dialog != null) {
dialog.setCanceledOnTouchOutside(false);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (dialog.getWindow() != null) {
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
}
}
baseView = inflater.inflate(R.layout.xxxx, null);
return baseView;
}
@Override
public void onStart() {
super.onStart();
final Dialog dialog = getDialog();
if (dialog != null) {
if (dialog.getWindow() != null) {
//設置寬高
dialog.getWindow().setLayout(WindowUtils.getScreenWidth(context) * 90 / 192, WindowManager.LayoutParams.WRAP_CONTENT);
}
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
EventBus.getDefault().register(this);
if (getArguments() != null) {
mOpenType = getArguments().getInt("type", 1);
}
initViews(baseView );
initData();
}
private void initData() {
//加載數據
}
private void initViews(View view) {
//設置dialog的高
mRootView = view.findViewById(R.id.xxxxxxx);
ViewGroup.LayoutParams params = mRootView.getLayoutParams();
params.height = WindowUtils.getScreenHeight(context) * 5 / 7;
mRootView.setLayoutParams(params);
//獲取view ,設置event
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
}
}
2 DialogFragment監聽返回鍵
實現DialogInterface.OnKeyListener 接口,監聽返回鍵。
implements DialogInterface.OnKeyListener {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final Dialog dialog = getDialog();
if (dialog != null) {
dialog.setCanceledOnTouchOutside(false);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (dialog.getWindow() != null) {
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
}
//添加監聽
dialog.setOnKeyListener(this);
}
return inflater.inflate(R.layout.xxx, null);
}
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
//此處捕獲back操作,如果不希望所在的Activity監聽到back鍵,需要返回true,消費掉。
backLogical();
dismissAllowingStateLoss();
return true;
}else {
//這裏注意當不是返回鍵時需將事件擴散,否則無法處理其他點擊事件
return false;
}
}
3 設置dialogFragment的寬高
@Override
public void onStart() {
super.onStart();
final Dialog dialog = getDialog();
if (dialog != null) {
if (dialog.getWindow() != null) {
//設置dailog的寬和高度模式
dialog.getWindow().setLayout(WindowUtils.getScreenWidth(context) * 90 / 192, WindowManager.LayoutParams.WRAP_CONTENT);
}
}
}
//然後initview時,獲取佈局文件根佈局,設置高度
ViewGroup.LayoutParams params = mRootView.getLayoutParams();
params.height = WindowUtils.getScreenHeight(context) * 5 / 7;
mRootView.setLayoutParams(params);