动态设置底部表格的最大扩展高度

时间:2018-03-17 08:11:58

标签: android bottom-sheet

How to set maximum expanded height in android support design bottom sheet?

问题是对上述问题的扩展,我想根据屏幕尺寸动态设置工作表的最大扩展高度。

我尝试将新的布局参数设置为实现底部表格行为的视图,但它没有任何好处。

4 个答案:

答案 0 :(得分:1)

请使用它并冷静:)

  1. const val BOTTOMSHEET_HEIGHT_TO_SCREEN_HEIGHT_RATIO = 0.80 //根据您的要求更改
  2. 在您的bottomsheetFragment 中覆盖onCreateDialog()

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
        dialog.setOnShowListener {
            dialog.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
                ?.apply {
                    val maxDesiredHeight =
                        (resources.displayMetrics.heightPixels * BOTTOMSHEET_HEIGHT_TO_SCREEN_HEIGHT_RATIO).toInt()
                    if (this.height > maxDesiredHeight) {
                        val bottomSheetLayoutParams = this.layoutParams
                        bottomSheetLayoutParams.height = maxDesiredHeight
                        this.layoutParams = bottomSheetLayoutParams
                    }
                    BottomSheetBehavior.from(this)?.apply {
                        this.state = BottomSheetBehavior.STATE_EXPANDED
                        this.skipCollapsed = true
                    }
                }
        }
        return dialog
    }

答案 1 :(得分:0)

终于找到了,

这个问题困扰了我很多,没有任何解决方案在任何地方报道,答案在于行为本身。

最小偏移量是底部应移动的最大值,我们将该值的下限设置为我们希望底部移动的所需高度。 您可以公开一个函数来设置值,或者在我们的行为中直接执行。

要动态设置底部表格的最大展开高度,我们需要在BottomSheetBehavior类中将最小偏移值从0增加到我们想要的值,让我显示代码。

快乐的编码!!

         // The minimum offset value upto which your bottomsheet to move
         private int mMinOffset;

        /**
          * Called when the parent CoordinatorLayout is about the layout the given child view.
          */
        @Override
        public boolean onLayoutChild(CoordinatorLayout parent, V child, int layoutDirection) {
               int dynamicHeight = Utils.dpToPx(parent.getContext(), **your_value_in_dp**);
               mMinOffset = Math.max(dynamicHeight, mParentHeight - child.getHeight());
               mMaxOffset = Math.max(mParentHeight - mPeekHeight, mMinOffset);
               mAnchorOffset = Math.min(mParentHeight - mAnchorHeight, mMaxOffset);

               if (mState == STATE_EXPANDED) {
                    ViewCompat.offsetTopAndBottom(child, mMinOffset);
                    anchorViews(mMinOffset);
               } 
        }

答案 2 :(得分:0)

最简单的解决方案是像这样设置底部工作表的maxHeight属性。

DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
 bottomSheet.setMaxHeight((int) (displayMetrics.heightPixels * 0.65));

答案 3 :(得分:0)

2021 我迟到了,但有人需要 Kotlin 扩展:

fun View.setupFullHeight(maxHeight: Double = 0.3) {
val displayMetrics = context?.resources?.displayMetrics
val height = displayMetrics?.heightPixels
val maximalHeight = (height?.times(maxHeight))?.toInt()
val layoutParams = this.layoutParams
maximalHeight?.let {
    layoutParams.height = it
}
this.layoutParams = layoutParams

}

使用方法:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    return object : BottomSheetDialog(requireContext(), R.style.DialogRoundedCornerStyle) {

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)

            dialog?.setOnShowListener {
                val bottomSheetDialog = it as BottomSheetDialog
                val parentLayout =
                    bottomSheetDialog.findViewById<View>(R.id.design_bottom_sheet)
                parentLayout?.let { view ->
                    val behavior = BottomSheetBehavior.from(view)
                    view.setupFullHeight()
                    behavior.apply {
                        state = BottomSheetBehavior.STATE_EXPANDED
                        isDraggable = false
                        isCancelable = false
                    }
                }
            }
        }

        override fun onBackPressed() {
            super.onBackPressed()
            dialog?.dismiss()
        }
    }
}