Android 修改BottomSheetDialog默认高度

前言

由于BottomSheetDialog内部setContentView时会添加进一个height=content_wrap的容器里,所以默认展示出来会是wrap,除非内容区域足够展示全屏,于是我们可以通过修改内部容器的高度来达到全屏。
关键源码如下:

@Override
  public void setContentView(View view) {
    super.setContentView(wrapInBottomSheet(0, view, null));
}

@Override
  public void setContentView(View view, ViewGroup.LayoutParams params) {
    super.setContentView(wrapInBottomSheet(0, view, params));
}

private View wrapInBottomSheet(
      int layoutResId, @Nullable View view, @Nullable ViewGroup.LayoutParams params) {
    ensureContainerAndBehavior();
    CoordinatorLayout coordinator = (CoordinatorLayout) container.findViewById(R.id.coordinator);
    if (layoutResId != 0 && view == null) {
      view = getLayoutInflater().inflate(layoutResId, coordinator, false);
    }

    FrameLayout bottomSheet = (FrameLayout) container.findViewById(R.id.design_bottom_sheet);
    if (params == null) {
      bottomSheet.addView(view);
    } else {
      bottomSheet.addView(view, params); // 这里的params只是决定传进来的view的layoutParms
    }

	......
}    

xml源码如下:design_bottom_sheet_dialog.xml

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

  <androidx.coordinatorlayout.widget.CoordinatorLayout
      android:id="@+id/coordinator"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true">

    <View
        android:id="@+id/touch_outside"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:importantForAccessibility="no"
        android:soundEffectsEnabled="false"
        tools:ignore="UnusedAttribute"/>

    <FrameLayout
        android:id="@+id/design_bottom_sheet"
        style="?attr/bottomSheetStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|top"
        app:layout_behavior="@string/bottom_sheet_behavior"/>

  androidx.coordinatorlayout.widget.CoordinatorLayout>

FrameLayout>

解决方案3步

在onStart里或show之后,修改layoutParams

  1. 修改外层window的宽高
  2. 修改com.google.android.material.R.id.design_bottom_sheet,FrameLayout的宽高
  3. 设置behavior.state为展开,BottomSheetBehavior.STATE_EXPANDED
 BottomSheetDialog(context).apply {
                create()
                setContentView(CommentDetailsListView(context, dialog = this).apply { setMediaInfo(resourceId, forwardId, commentInfo) }, ViewGroup.LayoutParams(-1,-1))
                show()
                 window?.apply {
                    // 1.修改window的高度
                    setLayout(-1, -1) // ViewGroup.LayoutParams.MATCH_PARENT
                    // 2.修改容器的高度
                    decorView.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)?.apply {
                        layoutParams.height = -1 
                        layoutParams.width = -1
                    }
                    // 3.设置状态为展开
                    behavior.state = BottomSheetBehavior.STATE_EXPANDED
                }
            }

你可能感兴趣的:(Kotlin,Android问题解决,android,kotlin)