DialogFragment的使用

来源:互联网 发布:知乎 庞麦郎 编辑:程序博客网 时间:2024/05/22 11:40

这里写图片描述
新建类AttachDialogFragment继承DialogFragment

public class AttachDialogFragment extends DialogFragment {    public Activity mContext;    @Override    public void onAttach(Activity activity){        super.onAttach(activity);        this.mContext = activity;    }}

TestFragment

public class TestFragment extends AttachDialogFragment{    @Override    public void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setStyle(DialogFragment.STYLE_NO_FRAME, R.style.CustomDatePickerDialog);    }    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        //设置无标题        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);        WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();        params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);        getDialog().getWindow().setAttributes(params);        View view = inflater.inflate(R.layout.fragment_test, container);        return view;    }    @Override    public void onStart() {        super.onStart();        //设置fragment高度 、宽度        int dialogHeight = (int) (mContext.getResources().getDisplayMetrics().heightPixels * 0.6);        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, dialogHeight);        getDialog().setCanceledOnTouchOutside(true);    }}

styles.xml

    <style name="CustomDatePickerDialog" parent="@style/AppTheme">        <item name="android:layout_width">match_parent</item>        <item name="android:layout_height">match_parent</item>        <item name="android:windowIsFloating">true</item>        <item name="android:windowAnimationStyle">@style/dialog_fragment_animation</item>        <item name="android:windowEnterAnimation">@anim/dialog_in_bottom</item>        <item name="android:windowExitAnimation">@anim/dialog_out_bottom</item>    </style>    <style name="dialog_fragment_animation">        <item name="android:windowEnterAnimation">@anim/dialog_in_bottom</item>        <item name="android:windowExitAnimation">@anim/dialog_out_bottom</item>    </style>

dialog_in_bottom.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <translate xmlns:android="http://schemas.android.com/apk/res/android"        android:duration="200"        android:fromYDelta="60%p"        android:toYDelta="0%p" /></set>

dialog_out_bottom.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <translate xmlns:android="http://schemas.android.com/apk/res/android"        android:duration="200"        android:fromYDelta="0%p"        android:toYDelta="60%p" /></set>

使用

findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                TestFragment playQueueFragment = new TestFragment();                playQueueFragment.show(getSupportFragmentManager(),"testframent");            }        });
0 0
原创粉丝点击