如何让 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)
设置对话框的状态和peekHeight
bottomSheetDialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
bottomSheetDialog.behavior.peekHeight = metrics.heightPixels
设置视图的高度,请注意我们如何将此高度设置为与对话框的 peekHeight 相同。当您想要 BottomSheetDialog 的单一尺寸时,我发现这是最好的方法
bottomSheet.layoutParams.height = metrics.heightPixels
bottomSheet.requestLayout()
首先在里面
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
}
}
// 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)
});
}
这是对我有用的最简单的解决方案。无需计算屏幕高度等。
override fun onStart() {
super.onStart()
val parentLayout = dialog?.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
parentLayout?.let { bottomSheet ->
val behaviour = BottomSheetBehavior.from(bottomSheet)
val layoutParams = bottomSheet.layoutParams
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT
bottomSheet.layoutParams = layoutParams
behaviour.state = BottomSheetBehavior.STATE_EXPANDED
}
}