一. 如何使用 DialogFragment

  1. 简介

    xxxxx

  2. 启动 DialogFragment

    • 获取 DialogFragment 的实例
    • 为实例设置 targetFragment
    • show DialogFragment

    代码如下:

    1
    2
    3
    4
    MyDialog dialog = MyDialog.getInstance(); // getInstance 是自己定义的,可以传参
    dialog.setTargetFragment(mTargetFragment, mRequestCode);
    FragmentActivity activity = (FragmentActivity) context;
    dialog.show(activity.getSupportFragmentManager(), mDialogTag);
  3. 向 DialogFragment 传入数据

    • 在 DialogFragment 中定义方法:getInstance()
    • getInstance() 定义调用 Dialog 时需要传入的参数类型
    • 调用 DialogFragment.getInstance() 获取实例的同时传入数据
    • 在需要获取传入数据的地方调用 getArguments()

    代码如下:

    对外接收数据的接口:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    public static MyDialog getInstance(String title, String[] promptInfo,
    String timerText, int totalSecond,
    Parcelable parcelable) {

    Log.i(TAG, "getInstance: ");
    MyDialog dialog = new MyDialog();

    // DialogFragment 能拿到传入数据的关键代码
    Bundle dialogArgs = new Bundle();
    dialogArgs.putString(TITLE, title);
    dialogArgs.putStringArray(PROMPT_INFOS, promptInfos);
    dialogArgs.putString(TIMER_TEXT, timerText);
    dialogArgs.putInt(TOTAL_SECOND, totalSecond);
    dialogArgs.putParcelable(PARCELABLE, parcelable);
    dialog.setArguments(dialogArgs);

    return dialog;
    }

    获取数据使用:

    1
    2
    3
    4
    5
    6
    7
    8
    // DialogFragment 中随处可调用
    Bundle bundle = getArguments();

    String[] propmtInfo = bundle.getStringArray(PROMPT_INFOS);
    String title = bundle.getString(TITLE);
    String timeText = bundle.getString(TIMER_TEXT);
    Parcelable parcelable = bundle.getParcelable(PARCELABLE);
    int totalSecond = bundle.getInt(TOTAL_SECOND);
  4. DialogFragment 传出数据

    • new 一个 Intent 的实例
    • 调用 Intent 的 putExtra()
    • 调用 targetFragmentonActivityResult() 方法将数据传出

    代码如下:

    1
    2
    3
    4
    5
    resultIntent = new Intent();
    resultIntent.putExtra(PARCELABLE, parcelable);
    mTargetFragment.onActivityResult(requestCode
    , Activity.RESULT_OK
    , resultIntent);

二. DialogFragment 一些常见的样式问题

  1. 设置背景透明

参考文档

  1. 对话框内部背景透明,在 onCreateView 中加入如下代码:

    1
    2
    // 也可以自定义背景图案
    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  2. 对话框外部背景透明,在 onStart 中加入如下代码:

    1
    2
    3
    4
    Window window = getDialog().getWindow();
    WindowManager.LayoutParams windowParams = window.getAttributes();
    windowParams.dimAmount = 0.0f;
    window.setAttributes(windowParams)
  3. 自定义 DialogFragment 宽度

参考文档

  1. 占用屏幕宽度一定比例,在 onStart 方法中加入如下代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    @Override
    public void onStart() {
    super.onStart();
    DisplayMetrics dm = new DisplayMetrics();
    if (mDialog != null) {
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = (int) (dm.widthPixels * 0.5);
    int height = (int) (dm.heightPixels * 0.6);
    mDialog.getWindow().setLayout(width, height);
    }
    }
  2. 去除标题栏