1. 程式人生 > >解決 資料不足一屏 BottomSheetDialog 無法全屏展示

解決 資料不足一屏 BottomSheetDialog 無法全屏展示

最近專案有遇到 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)
    }
}
實現了全屏