美文网首页
DialogFragment实现底部弹窗

DialogFragment实现底部弹窗

作者: 楷桐 | 来源:发表于2017-10-26 18:05 被阅读654次

    挖井人:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2016/1128/6818.html

    • DialogFragment是3.0之后提供的一个弹出框实现类。使用DialogFragment的好处是能够更好的控制其生命周期。

    创建一个DialogFragment

    public class BottomDialog extends DialogFragment {
        public static BottomDialog newInstance() {
            Bundle args = new Bundle();
            BottomDialog fragment = new BottomDialog();
            fragment.setArguments(args);
            return fragment;
        }
        @Override
        public void onStart() {
            super.onStart();
            Window window = getDialog().getWindow();
            WindowManager.LayoutParams params = window.getAttributes();
            params.gravity = Gravity.BOTTOM;
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            window.setAttributes(params);
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
            View view = inflater.inflate(R.layout.dialog_bottom,container,false);
            AnimationUtils.slideToUp(view);
            return view;
        }
    }
    

    对应布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical"
                  android:padding="@dimen/activity_horizontal_margin"
    android:background="@color/colorAccent">
        <TextView android:layout_width="match_parent" android:layout_height="48dp"
                  android:gravity="center_vertical"
                  android:text="bottomSheet"/>
    </LinearLayout>
    

    加载动画

    public static void slideToUp(View view){
            Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                    Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                    1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
            slide.setDuration(400);
            slide.setFillAfter(true);
            slide.setFillEnabled(true);
            view.startAnimation(slide);
            slide.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }
                @Override
                public void onAnimationEnd(Animation animation) {
                }
                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });
        }
    

    相关文章

      网友评论

          本文标题:DialogFragment实现底部弹窗

          本文链接:https://www.haomeiwen.com/subject/bxgkpxtx.html