簡體   English   中英

android支持設計底頁如何設置最大展開高度?

[英]How to set maximum expanded height in android support design bottom sheet?

我已經展示了我的底頁布局,其 peek 高度設置為 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

祝你好運和快樂編碼!

您可以將參數“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

以上解決方案對我不起作用,所以我解決了

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

如果使用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;
}

出於某種原因,接受的答案對我不起作用。 也許是因為我沒有使用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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM