【问题标题】:Set BottomSheetDialogFragment height to full screen将 BottomSheetDialogFragment 高度设置为全屏
【发布时间】:2020-02-26 00:09:11
【问题描述】:

如何使 bottomSheet 占据屏幕的整个高度?设置窥视高度无效。

任何帮助将不胜感激。

bottomSheetDialogFragment.getDialog().setOnShowListener((dialog) ->
{
    final BottomSheetDialog bottomSheetDialog = (BottomSheetDialog)dialog;
    final FrameLayout bottomSheet = bottomSheetDialog.findViewById(R.id.design_bottom_sheet);
    if (bottomSheet != null)
    {
        final BottomSheetBehavior<View> behavior = BottomSheetBehavior.from(bottomSheet);
        behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        behavior.setPeekHeight(30000); // no effect, bottom sheet does not span entire height of screen
    }
});

底页布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    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/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

    <!-- rest of layout not shown -->
    <FrameLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/bottomSheetHandle"
        tools:layout_height="48dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

标签: android bottom-sheet android-bottomsheetdialog


【解决方案1】:

您可以获取指标以访问以像素为单位的屏幕高度,并使用该参考来设置底页的高度。

获取指标

val metrics = DisplayMetrics()
requireActivity().windowManager?.defaultDisplay?.getMetrics(metrics)

设置对话框的状态和 peekHeight

bottomSheetDialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
bottomSheetDialog.behavior.peekHeight = metrics.heightPixels

设置视图的高度,注意我们是如何将这个高度设置为与对话框的 peekHeight 相同的。当您想要为 BottomSheetDialog 提供单一尺寸时,​​我发现这是最好的方法

bottomSheet.layoutParams.height = metrics.heightPixels
bottomSheet.requestLayout()

【讨论】:

    【解决方案2】:

    一开始在onCreateDialog

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        ...
        bottomSheetBehavior?.skipCollapsed = true
        bottomSheetBehavior?.peekHeight = Resources.getSystem().displayMetrics.heightPixels
        bottomSheetBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
        return bottomSheet
    }
    

    之后,在启动方法上使用 this

    /**
     * to make sheet height full screen
     * */
    override fun onStart() {
        super.onStart()
        val metrics = DisplayMetrics()
        requireActivity().windowManager?.defaultDisplay?.getMetrics(metrics)
        binding.rootContainer.layoutParams.height = metrics.heightPixels
        binding.rootContainer.requestLayout()
    }
    

    我希望它能正常工作,因为它可以正常工作;)

    【讨论】:

      【解决方案3】:
      // add this code into your class
      
          @Override
              public void onStart() {
                  super.onStart();
                  Dialog dialog = getDialog();
                  View bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);
                  if (dialog != null) {
              
                      bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
                  }
                  View view = getView();
                  view.post(() -> {
                      View parent = (View) view.getParent();
                      CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) (parent).getLayoutParams();
                      CoordinatorLayout.Behavior behavior = params.getBehavior();
                      BottomSheetBehavior bottomSheetBehavior = (BottomSheetBehavior) behavior;
                      bottomSheetBehavior.setPeekHeight(view.getMeasuredHeight());
                      ((View)bottomSheet.getParent()).setBackgroundColor(Color.TRANSPARENT)
          
                  });
              }
      

      【讨论】:

        【解决方案4】:

        要实现这一点,您可以将 match_parent 设置为底部的布局参数 像这样的表:

        override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
                            val dialog = BottomSheetDialog(requireContext(), theme)
                            dialog.setOnShowListener {
                        
                                val bottomSheetDialog = it as BottomSheetDialog
                                val parentLayout =
                                    bottomSheetDialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
                                parentLayout?.let { it ->
                                    val behaviour = BottomSheetBehavior.from(it)
                                    setupFullHeight(it)
                                    behaviour.state = BottomSheetBehavior.STATE_EXPANDED
                                }
                            }
                            return dialog
                        }
                        
                        private fun setupFullHeight(bottomSheet: View) {
                            val layoutParams = bottomSheet.layoutParams
                            layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT
                            bottomSheet.layoutParams = layoutParams
                        }
            }
        

        【讨论】:

        • 纯代码答案是低质量的答案。如果您添加一些解释,那就太棒了。
        猜你喜欢
        • 1970-01-01
        • 2018-05-08
        • 1970-01-01
        • 2011-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-10
        相关资源
        最近更新 更多