如何让 bottomSheet 占据整个屏幕的高度?设置 peek 高度无效。
如有任何帮助,我们将不胜感激。
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
}
});
Bottom Sheet 布局
<?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>
最佳答案
您可以获取指标以访问屏幕的像素高度,并使用该引用来设置 Bottom Sheet 的高度。
获取指标
val metrics = DisplayMetrics()
requireActivity().windowManager?.defaultDisplay?.getMetrics(metrics)
设置对话框的状态和 peekHeight
bottomSheetDialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
bottomSheetDialog.behavior.peekHeight = metrics.heightPixels
设置 View 的高度,请注意我们如何将此高度设置为与对话框的 peekHeight 相同。当您想要 BottomSheetDialog 的单一尺寸时,我发现这是最好的方法
bottomSheet.layoutParams.height = metrics.heightPixels
bottomSheet.requestLayout()
关于android - 将 BottomSheetDialogFragment 高度设置为全屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60404985/