DialogFragment使用时遇到的一些问题

来源:互联网 发布:局域网监控软件排行 编辑:程序博客网 时间:2024/05/16 12:43

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()中设置才有效.
@Overridepublic 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中引入自定义动画<style name="CustomDialog" parent="@android:style/Theme.Dialog">    <item name="android:windowEnterAnimation">@anim/popwin_show_anim</item>    <item name="android:windowExitAnimation">@anim/popwin_hide_anim</item></style>//在Java代码中设置窗口动画getDialog().getWindow().getAttributes().windowAnimations = R.style.CustomDialog;  
  • popwin_show_anim.xml代码
<?xml version="1.0" encoding="utf-8"?><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代码
<?xml version="1.0" encoding="utf-8"?><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);
1 0
原创粉丝点击