【问题标题】:How to set maximum expanded height in android support design bottom sheet?如何在android支持设计底页中设置最大展开高度?
【发布时间】:2016-04-15 01:12:13
【问题描述】:

我已经展示了我的底部工作表布局,其窥视高度设置为 100dp。但是我怎样才能限制我的底页只扩展到 500dp 呢?这是我的示例布局:

<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsActivity" />

<android.support.v4.widget.NestedScrollView
    android:id="@+id/design_bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    app:behavior_hideable="true"
    app:behavior_peekHeight="100dp"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_marginBottom="5dp"
            android:background="#e444ff" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_marginBottom="5dp"
            android:background="#e444ff" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_marginBottom="5dp"
            android:background="#e444ff" />

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_margin="@dimen/activity_horizontal_margin"
    app:layout_anchor="@+id/design_bottom_sheet"
    app:layout_anchorGravity="top|right|end"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</android.support.design.widget.CoordinatorLayout>

除了我的问题,如何禁止用户上下拖动底部工作表?

【问题讨论】:

  • stackoverflow.com/a/49527832/3439133 如果您想动态更改最大高度,请选中此项
  • android:minHeight="100dp" android:maxHeight="500dp" 同时使用最大值和最小值。 min 用于防止底页消失。

标签: android android-support-design


【解决方案1】:

要阻止底部工作表向上移动全屏很简单,只需将 NestedScrollView 的 layout_height 设置为 500dp,您可能还想将其设置为 layout_gravity="bottom"

<android.support.v4.widget.NestedScrollView
    android:id="@+id/design_bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="500dp"
    android:background="#000000"
    android:layout_gravity="bottom"
    app:behavior_hideable="true"
    app:behavior_peekHeight="100dp"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_marginBottom="5dp"
            android:background="#e444ff" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_marginBottom="5dp"
            android:background="#e444ff" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_marginBottom="5dp"
            android:background="#e444ff" />

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

如果您不希望视图可向上拖动,则需要将 behavior_peekHeightlayout_height 设置为相同的值。

要阻止视图向下拖动,只需将 behavior_hideable 标志设置为 false

【讨论】:

  • 太好了!这些是非常聪明的解决方案。非常感谢!
  • 你忘记了我们应该将 app:behavior_hideable 设置为 false 的内容,这样我们就可以确保底部的工作表不可拖动
  • 不幸的是,这并没有解决我的底部工作表高度。 !
  • 如何在模态底板中实现这一点(限制底板的可扩展高度)?
  • 嘿@inner_class7,我们如何动态更改此最大高度而不是在xml中设置。我尝试为布局设置新的布局参数,但它崩溃了。
【解决方案2】:

您可以将参数“maxHeight”设置为底部工作表的根视图组。

android:maxHeight=500dp

使用 ConstraintLayout 作为根布局效果很好。

【讨论】:

  • 不幸的是,不适用于嵌套的RecyclerView
【解决方案3】:

以上解决方案对我不起作用,所以我解决了它

1.在底部表格布局的根视图中添加app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"

2.自定义底页样式:

<style name="AppBottomSheetDialogTheme" parent="Theme.MaterialComponents.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/AppModalStyle</item>
    </style>

    <style name="AppModalStyle" parent="Widget.MaterialComponents.BottomSheet">
        <item name="behavior_peekHeight">600dp</item>
    </style>

3. 并在 Activity 或 Fragment 中使用它

    val mBottomSheetDialog = BottomSheetDialog(this, R.style.AppBottomSheetDialogTheme)
    val inflater = this.layoutInflater
    val sheetView = inflater.inflate(R.layout.bottom_sheet_layout, null)
    mBottomSheetDialog.setContentView(sheetView)
    mBottomSheetDialog.show()

【讨论】:

    【解决方案4】:

    如果使用 BottomSheetDialogFragment:

    在我的例子中,我使用的是 BottomSheetDialogFragment,并通过在我的 BottomSheetFragment 中覆盖 OnCreateDialog 方法获取它的引用来修改底部工作表窥视高度

    @NonNull @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    
        dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
    
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                BottomSheetDialog bottom_dialog = (BottomSheetDialog) dialog;
    
                FrameLayout bottomSheet = (FrameLayout) bottom_dialog.findViewById(com.google.android.material.R.id.design_bottom_sheet);
                assert bottomSheet != null;
                //BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
                DisplayMetrics displayMetrics = requireActivity().getResources().getDisplayMetrics();
                int height = displayMetrics.heightPixels;
                int maxHeight = (int) (height*0.80);
                BottomSheetBehavior.from(bottomSheet).setPeekHeight(maxHeight);
            }
        });
    
        return dialog;
    }
    

    【讨论】:

      【解决方案5】:

      由于某种原因,接受的答案对我不起作用。可能是因为我没有使用fragmentNestedScrollView 作为BottomSheet 中的布局。无论如何,就我而言,解决方案是限制CoordinatorLayout 的大小:

      <ConstraintLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent">
         <ConstraintLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent">
              <!-- Your main content here -->
         </ConstarintLayout>
         <CoordinatorLayout
              android:layout_width="match_parent"
              android:layout_height="MAX SIZE OF BOTTOM SHEET">
              <YourBottomSheetLayout>
                   app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"/>
         </CoordinatorLayout>
      </ConstarintLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-10-12
        • 2016-10-26
        • 1970-01-01
        • 1970-01-01
        • 2020-02-10
        • 1970-01-01
        • 1970-01-01
        • 2017-07-30
        相关资源
        最近更新 更多