如何在android支持设计底部页面中设置最大扩展高度



我已经展示了我的底层布局,它的窥视高度设置为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>

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

要阻止底部页面向上移动到全屏很简单,只需将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

您只需设置param"maxHeight";到底部工作表的根视图组。

android:maxHeight="500dp"

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

您可以使用BottomSheetBehavior中的setMaxHeight方法(此方法应在show()方法之前调用)

bottomSheetDialog.behavior.maxHeight = 1000 // set max height when expanded in PIXEL
bottomSheetDialog.behavior.peekHeight = 400 // set default height when collapsed in PIXEL

如果使用BottomSheetDialogFragment:

在我的案例中,我使用了BottomSheetDialogFragment,并通过覆盖BottomSheetFragment 中的OnCreateDialog方法来获得其引用,从而修改了底部工作表的peek高度

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

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

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.并通过在"活动"或"片段"中使用

    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()

如果您在内部使用约束布局和回收视图,则可以使用此

app:layout_constraintHeight_max="500dp"
app:layout_constraintHeight_min="500dp"

在recyclerView中。

类似这样的东西:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="500dp">
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintToptoTopOf="parent"
        app:layout_constraintHeight_max="500dp"
        app:layout_constraintHeight_min="500dp"/>
</ConstraintLayout>

由于某种原因,接受的答案对我不起作用。也许是因为我没有使用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>

最新更新