繁体   English   中英

将 BottomSheetDialogFragment 高度设置为全屏

[英]Set BottomSheetDialogFragment height to full screen

如何让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>

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

获取指标

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

设置对话框的 state 和 peekHeight

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

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

bottomSheet.layoutParams.height = metrics.heightPixels
bottomSheet.requestLayout()
// 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)
    
            });
        }

首先在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()
}

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

为此,您可以将 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
                }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM