DialogFragment实现底部弹窗

挖井人: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;
    }
}

对应布局



    

加载动画

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实现底部弹窗)