首页 > 解决方案 > 如何修复底部工作表对话框的出现位置

问题描述

在 Android 11 中,BottomSheetDialog(BSD) 看起来像它应该的那样,但在旧版本上它要么在它上面,要么Navigation Bar在它下面。我附上了我的问题的图片,这是我的 xml:

<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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/modal_bottom_sheet_background"
    android:backgroundTint="?attr/backgroundColor"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"
    android:layout_gravity="bottom"
    app:layout_behavior="android.support.design.widget.BottomSheetBehaviorvior"
    >
   

    <androidx.cardview.widget.CardView
        android:id="@+id/fab_tabliceID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/layout_container_margin_start_end_16"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="@dimen/layout_container_margin_start_end_16"
        app:cardCornerRadius="@dimen/card_corner_radius_15"
        app:layout_constraintTop_toTopOf="parent"
        >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:padding="8dp"...

这是当我试图将所有内容都包含在 Coordinator Layout 中时,但它没有用:

<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:layout_height="match_parent">

            <androidx.constraintlayout.widget.ConstraintLayout

                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:background="@drawable/modal_bottom_sheet_background"
                android:backgroundTint="?attr/backgroundColor"
                app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
                <!--android:background="@android:color/transparent"-->
                <!--android:layout_gravity="bottom"-->

                <androidx.cardview.widget.CardView
                    android:id="@+id/fab_tabliceID"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/layout_container_margin_start_end_16"
                    android:layout_marginTop="32dp"
                    android:layout_marginEnd="@dimen/layout_container_margin_start_end_16"
                    app:cardCornerRadius="@dimen/card_corner_radius_15"
                    app:layout_constraintTop_toTopOf="parent">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center"...

当然,我想到的解决方案之一是在代码中检查 Android 版本,然后,如果需要,添加底部边距,但这看起来不是正确的方法。

在此处输入图像描述

标签: androidandroid-bottomsheetdialogbottomsheetdialogfragment

解决方案


推荐阅读