2

如何使 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>
4

4 回答 4

3

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

获取指标

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()
于 2020-07-01T21:32:38.277 回答
1

起初在里面onCreateDialog

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

之后,在启动方法上使用这个

/**
 * 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()
}

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

于 2021-07-05T18:10:27.017 回答
0
// 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)
    
            });
        }
于 2021-05-31T11:29:00.137 回答
0

为此,您可以将 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
                }
    }
于 2021-08-30T07:11:23.097 回答