简体   繁体   中英

Set BottomSheetDialogFragment height to full screen

How do I make a bottomSheet take up the full height of the screen? Setting the peek height has no effect.

Any help would be appreciated.

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
    }
});

BottomSheet Layout

<?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>

You could get the metrics to have access to the height of the screen in pixels and use that reference to set the height of your bottomsheet.

Get Metrics

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

Set state and peekHeight of dialog

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

Set height of your view, notice how we are setting this height as the same of the peekHeight of the dialog. I found this the best way when you want a single size for your 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)
    
            });
        }

at first inside onCreateDialog

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

afterward, use this on start method

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

I hope it works fine because it work correctly with me ;)

to accomplish that you can set match_parent to layout params of bottom sheet like this:

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
                }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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