版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013762572/article/details/79648159
问题详细出现以及原因可以参考这篇文章。
主要给出解决方法,主要是针对BottomSheetDialogFragment的显示问题,而非BottomSheetDialog,其实解决方案是类似的。
重写BottomSheetDialog
,重新计算peek高度,具体代码如下:
public class FixHeightBottomSheetDialog extends BottomSheetDialog {
private View mContentView;
public FixHeightBottomSheetDialog(@NonNull Context context) {
super(context);
}
public FixHeightBottomSheetDialog(@NonNull Context context, int theme) {
super(context, theme);
}
@Override
protected void onStart() {
super.onStart();
fixHeight();
}
@Override
public void setContentView(View view) {
super.setContentView(view);
this.mContentView = view ;
}
@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
super.setContentView(view, params);
this.mContentView = view;
}
private void fixHeight(){
if(null == mContentView){
return;
}
View parent = (View) mContentView.getParent();
BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
mContentView.measure(0, 0);
behavior.setPeekHeight(mContentView.getMeasuredHeight());
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) parent.getLayoutParams();
params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
parent.setLayoutParams(params);
}
}
然后再继承BottomSheetDialogFragment
,重写onCreateDialog
方法,替换为上面FixHeightBottomSheetDialog
,代码如下:
public class FixBottomSheetDialogFragment extends BottomSheetDialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new FixHeightBottomSheetDialog(getContext());
}
}
那么此时FixBottomSheetDialogFragment.show()就可以显示全部了,问题解决。
FixBottomSheetDialogFragment fragment = new FixBottomSheetDialogFragment();
fragment.show(getSupportFragmentManager(),"myFragment");