android - 将 BottomSheetDialogFragment 高度设置为全屏

标签 android bottom-sheet android-bottomsheetdialog

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

相关文章:

安卓网络接口(interface)。 NetworkInterface 名称的含义是什么?

android - 是否可以更改手机的 API 级别并使用 Android 应用程序测试我们的应用程序?

android - 在菜单抽屉/滑动菜单中突出显示所选项目

flutter - 在下拉菜单中显示带有下拉选项的 Bottom Sheet 单击 flutter

android - 填充 RecyclerView 后,底部工作表对话框跳起来

从 ram 清除时,Android 广播不执行

android - 如何使用 Dagger2 将 ViewModel 注入(inject) BottomSheetDialogFragment?

android-layout - 键盘出现时 Bottom Sheet 对话框片段未向上滚动

android - 向 BottomSheetDialogFragment 添加边距

java - 在 Custom BottomSheetDialog Android 中添加菜单