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
                }
            }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柯基爱蹦跶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值