android design包的BottomSheetDialogFragment控件设置高度的方法

BottomSheetDialogFragment如果按照其他博客的使用方法,直接用onCreateView方法设置布局,初始化的话,而你的布局中正好有listview recyclerview这种列表控件的话,弹出来的高度是有问题的,解决高度不能完全显示的方法是,删掉onCreateView方法,采用如下代码初始化:

 BottomSheetBehavior mBottomSheetBehavior;
    @Override
    public void setupDialog(Dialog dialog, int style) {
        super.setupDialog(dialog, style);
        final View root = View.inflate(getContext(), R.layout.landscape_living_test_fragment, null);
        dialog.setContentView(root);
        ButterKnife.bind(this,root);
        init();
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) root.getParent()).getLayoutParams();
        CoordinatorLayout.Behavior behavior = params.getBehavior();

        if (behavior != null && behavior instanceof BottomSheetBehavior) {
            mBottomSheetBehavior = (BottomSheetBehavior) behavior;
            mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                @Override
                public void onStateChanged(@NonNull View bottomSheet, int newState) {

                }

                @Override
                public void onSlide(@NonNull View bottomSheet, float slideOffset) {

                }
            });

            root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    root.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    int height = root.getMeasuredHeight();
                    mBottomSheetBehavior.setPeekHeight(height);
                }
            });
        }
    }

可以看到重写了setupDialog方法,landscape_living_test_fragment则是我自己项目界面的布局,这样一搞就OK了,

原文地址:解决BottomSheetDialogFragment内容显示不全



你可能感兴趣的:(安卓开发)