简体   繁体   English

在 android 中为底页对话框片段设置固定高度

[英]Set fixed height for bottom sheet dialog fragment in android

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:id="@+id/layout"
    style="@style/AppBottomSheetDialogTheme"
    android:layout_height="match_parent"
    tools:context=".fragments.bottomsheet.FilterBottomSheetFragment">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp"
            android:orientation="vertical"
            android:paddingHorizontal="15dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingHorizontal="10dp"
                android:text="Payment method"
                android:textColor="@color/black"
                android:textSize="20dp"
                android:textStyle="bold" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rcPaymentMethod"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp" />

            <View
                style="@style/Divider.Horizontal"
                android:layout_marginTop="15dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:paddingHorizontal="10dp"
                android:text="Bed Size"
                android:textColor="@color/black"
                android:textSize="20dp"
                android:textStyle="bold" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rcBedSize"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp" />

            <View
                style="@style/Divider.Horizontal"
                android:layout_marginTop="15dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:paddingHorizontal="10dp"
                android:text="View"
                android:textColor="@color/black"
                android:textSize="20dp"
                android:textStyle="bold" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rcView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp" />

            <View
                style="@style/Divider.Horizontal"
                android:layout_marginTop="15dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:paddingHorizontal="10dp"
                android:text="Facilities"
                android:textColor="@color/black"
                android:textSize="20dp"
                android:textStyle="bold" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rcFacilities"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp" />
            </LinearLayout>
        </androidx.core.widget.NestedScrollView>


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginHorizontal="13dp"
            android:layout_marginVertical="10dp">

            <TextView
                android:id="@+id/txtClearAll"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:clickable="true"
                android:text="Clear All"
                android:textColor="@color/black"
                android:textSize="18dp" />

            <Button
                android:id="@+id/btnApply"
                android:layout_width="130dp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:background="@drawable/rounded_corner_button"
                android:text="Apply"
                android:textAllCaps="false"
                android:textColor="@color/white"
                app:backgroundTint="@color/primary" />
        </RelativeLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

This is file xml of bottom sheet dialog fragment.这是底部表单对话框片段的文件 xml。 I want to set fixed height for fragment when it showed in other fragment.当它显示在其他片段中时,我想为片段设置固定高度。 Because in bottom sheet I have recycler view so when I scroll bottom sheet expanded to full screen.因为在底部工作表中我有回收站视图所以当我滚动底部工作表时扩展到全屏。 I don't want that.我不想要那个。 I want to set fixed height for it.我想为它设置固定高度。 And I want to button Apply always display at the bottom when I scrool.而且我想在滚动时按钮“应用”始终显示在底部。

说明

To full height:全高:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    val dialog = context?.let { BottomSheetDialog(it, theme) }
    dialog?.setOnShowListener {
        val bottomSheetDialog = it as BottomSheetDialog
        val parentLayout =
            bottomSheetDialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
        parentLayout?.let { bottomSheet ->
            val behaviour = BottomSheetBehavior.from(bottomSheet)
            setupFullHeight(bottomSheet)
            behaviour.state = BottomSheetBehavior.STATE_EXPANDED
        }
    }
    return dialog ?: super.onCreateDialog(savedInstanceState)
}

private fun setupFullHeight(bottomSheet: View) {
    val layoutParams = bottomSheet.layoutParams
    layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT
    bottomSheet.layoutParams = layoutParams
}
  • button Apply, You can set top of view, or stick to bottom use RelativeLayout按钮应用,你可以设置视图的顶部,或者坚持底部使用 RelativeLayout

Add Layout custom fix height添加 Layout 自定义固定高度

android:layout_height="500dp" android:layout_height="500dp"

Add Layout peek height添加布局 peek 高度

app:behavior_peekHeight="100dp" app:layout_behavior="android.support.design.widget.BottomSheetBehavior" app:behavior_peekHeight="100dp" app:layout_behavior="android.support.design.widget.BottomSheetBehavior"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM