使用DialogFragment实现底部菜单

作者: 猿圆猿 | 来源:发表于2016-09-25 18:39 被阅读5734次

效果图

device-2016-09-25-174226.gif

实现

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;
    }


}

对应的layout:

<?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>

加载自定义布局


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.dialog_bottom,container,false);
        AnimationUtils.slideToUp(view);
        return view;
    }

跟使用Fragment一样,重写onCreateView方法。

getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

的作用是除掉弹出框的标题。

设置DialogFragment的位置

一把我们的弹出框默认都是居中显示的,现在我们需要的是底部显示,只要把LayoutParams的gravity属性设置为Bottom。

    @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));
    }

加载动画

AnimationUtils.slideToUp(view);
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) {

            }
        });

    }

相关阅读:使用BottomSheet实现底部菜单
源码地址:BottomMenuTutorial ,欢迎start,谢谢。

相关文章

网友评论

  • TripleZhao:请问一下!为什么我一样设置的是 Match_Parent和Gravity.Bottom属性,显示的时候却不是占满屏幕宽度和屏幕底部对齐呢?急~~~:sob:
  • zdmelon:距离底部的margin怎么设置
    猿圆猿:<dimen name="bottom_lib_dialog_padding_top">12dp</dimen>
    <dimen name="bottom_lib_dialog_padding_bottom">12dp</dimen>
    <dimen name="bottom_lib_dialog_padding_left">12dp</dimen>
    <dimen name="bottom_lib_dialog_padding_right">12dp</dimen>
    猿圆猿:升级到最新版本1.1.0版本,然后在dimens.xml中去自定义padding
  • sendtion:不错
    猿圆猿:@sendtion 谢谢
  • 0fbb30205f52:你试试快速点几下按钮看会不会崩溃
    猿圆猿:@Gbravo 试了一下,正常的,你那边会崩溃的么
  • 冬虫不可以语夏:666
    猿圆猿:@不知寒号 谢谢

本文标题:使用DialogFragment实现底部菜单

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