美文网首页
解决bottomsheetdialog 无法显示完全的问题

解决bottomsheetdialog 无法显示完全的问题

作者: wang_ang | 来源:发表于2019-06-07 11:04 被阅读0次

我们的项目中,分享弹窗都是用的bottomsheetdialog,但是会出现一个问题,就是在横屏是,弹窗只能显示一半,需要手动往上滑一下才能完全显示,看源码发现
@Override
protected void onStart() {
super.onStart();
if (mBehavior != null) {
mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}

在bottomsheetdialog的onstart方法中设置了它的behavior为收缩状态 ,再看这个behavior的创建方式

   mBehavior = BottomSheetBehavior.from(bottomSheet);

    public static <V extends View> BottomSheetBehavior<V> from(V view) {
    ViewGroup.LayoutParams params = view.getLayoutParams();
    if (!(params instanceof CoordinatorLayout.LayoutParams)) {
        throw new IllegalArgumentException("The view is not a child of CoordinatorLayout");
    }
    CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) params)
            .getBehavior();
    if (!(behavior instanceof BottomSheetBehavior)) {
        throw new IllegalArgumentException(
                "The view is not associated with BottomSheetBehavior");
    }
    return (BottomSheetBehavior<V>) behavior;
}

就是获取到coordinatelayout 中的behavior ,知道了这些,就很容易解决这个问题了;

先获取behavior :

     ViewParent parent = contentView.getParent();
    
      mBehavior = BottomSheetBehavior.from(((FrameLayout) parent));

再重写onstart方法 :

       @Override
protected void onStart() {
    super.onStart();
    if (mBehavior != null) {
        mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    }
}

相关文章

网友评论

      本文标题:解决bottomsheetdialog 无法显示完全的问题

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