我们的项目中,分享弹窗都是用的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);
}
}