DialogFragment使用时遇到的一些问题

DialogFragment使用时遇到的一些问题

官方文档:
https://developer.android.com/reference/android/app/DialogFragment.html

Activity向Dialog传递参数

public static MyDialogFragment newInstance(int num) {
        MyDialogFragment f = new MyDialogFragment();
        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);
        return f;
    }
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNum = getArguments().getInt("num");
}

设置宽高和对话框背景等

  • 在xml中设置和onCreateView(), onViewCreated()中设置无效. 在onStart()和onResume()中设置才有效.
@Override
public void onStart() { //在onStart()中
    super.onStart();
    getDialog().getWindow().setBackgroundDrawableResource(R.drawable.background); //对话框背景
    getDialog().getWindow().setLayout(300,200); //宽高
}

官方示例的显示对话框的方式

void showDialog() {
    mStackLevel++;
    // DialogFragment.show() will take care of adding the fragment
    // in a transaction.  We also want to remove any currently showing
    // dialog, so make our own transaction and take care of that here.
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    Fragment prev = getFragmentManager().findFragmentByTag("dialog");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);
    // Create and show the dialog.
    DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
    newFragment.show(ft, "dialog");
}

去除标题栏

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); //去除标题栏
        return inflater.inflate(R.layout.dialog, container, false);
    }

设置点击外部/返回键不消失

//点击外部不消失getDialog.setCanceledOnTouchOutside(false);
//点击返回键不消失,需要监听OnKeyListener:
getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });

设置Dialog位于屏幕底部

Window window = getDialog().getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.gravity = Gravity.BOTTOM; //底部
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(lp);

//设置对话框宽高也可以使用lp.width和lp.height

Dialog向Activity传参

//利用接口传参,在DialogFragment中定义接口
public interface DialogListener {
   void onComplete(String result);
}

//Activity实现该接口
public class MyActivity extends Activity implements DialogListener {
  // ...
  @Override
  public void onComplete(String result){
    //使用参数
  }
}

//在DialogFragment中传参数
DialogListener listener=(DialogListener)getActivity();
listener.onComplete(result); //传参数

Enter和Exit动画 (飞入飞出)

//在style.xml中引入自定义动画


//在Java代码中设置窗口动画
getDialog().getWindow().getAttributes().windowAnimations = R.style.CustomDialog;  
  • popwin_show_anim.xml代码

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="300"
        android:fromYDelta="100%p"
        android:toYDelta="0" />

    <alpha
        android:duration="300"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
set>
  • popwin_hide_anim.xml代码

<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:duration="300"
        android:fromYDelta="0"
        android:toYDelta="50%p"/>

    <alpha
        android:duration="300"
        android:fromAlpha="1.0"
        android:toAlpha="0.8"/>
set>

设置背景Activity的明暗度

// 0~1 , 1表示完全昏暗
getDialog().getWindow().setDimAmount(0.8f);

你可能感兴趣的:(安卓天坑)