最近项目有遇到 BottomSheetDialog 展示内容未满屏无法显示的情况
首先查看到BottomSheetDialog 设置了全屏
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setLayout( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); }
通过
@Override public void setContentView(View view, ViewGroup.LayoutParams params) { super.setContentView(wrapInBottomSheet(0, view, params)); }
进入
wrapInBottomSheet
private View wrapInBottomSheet(int layoutResId, View view, ViewGroup.LayoutParams params) { final CoordinatorLayout coordinator = (CoordinatorLayout) View.inflate(getContext(), R.layout.design_bottom_sheet_dialog, null); if (layoutResId != 0 && view == null) { view = getLayoutInflater().inflate(layoutResId, coordinator, false); } FrameLayout bottomSheet = (FrameLayout) coordinator.findViewById(R.id.design_bottom_sheet); mBehavior = BottomSheetBehavior.from(bottomSheet); mBehavior.setBottomSheetCallback(mBottomSheetCallback); mBehavior.setHideable(mCancelable); if (params == null) { bottomSheet.addView(view); } else { bottomSheet.addView(view, params); }发现自己所写的view是添加到
FrameLayout bottomSheet = (FrameLayout) coordinator.findViewById(R.id.design_bottom_sheet);
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <View android:id="@+id/touch_outside" android:layout_width="match_parent" android:layout_height="match_parent" android:soundEffectsEnabled="false"/> <FrameLayout android:id="@+id/design_bottom_sheet" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|top" android:clickable="true" app:layout_behavior="@string/bottom_sheet_behavior" style="?attr/bottomSheetStyle"/> </android.support.design.widget.CoordinatorLayout>
<FrameLayout android:id="@+id/design_bottom_sheet" android:layout_width="match_parent" android:layout_height="wrap_content"
设置的高度是自适应
这边通过复写
BottomSheetDialog
override fun show() { super.show() window.apply { var mContent = decorView.findViewById(R.id.design_bottom_sheet) var orginLayoutParams = mContent.layoutParams orginLayoutParams.height=ViewGroup.LayoutParams.MATCH_PARENT mContent.layoutParams=orginLayoutParams val mDialogBehavior = BottomSheetBehavior.from(mContent) mDialogBehavior.setState(BottomSheetBehavior.STATE_EXPANDED) } }实现了全屏