为什么小吃店占满宽度

Why snackbar is taken full widht

我在 BottomSheetDialogFragment 中展示了一个小吃店,我期待它的正常行为。

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

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:id="@+id/bottomSheetRootView"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/interestSheetTitleViewGroup"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@drawable/bottom_sheet_backgroud_transition"
        android:elevation="2dp"
        android:outlineProvider="bounds"
        app:layout_constraintBottom_toTopOf="@+id/expandedInterestView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/interestSheetTitleView"
            style="@style/EstadaoTextView.HeadLineMedium"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:ellipsize="end"
            android:gravity="center_vertical"
            android:maxLines="1"
            android:paddingStart="16dp"
            android:paddingEnd="16dp"
            android:textColor="@color/colorAccent"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="Title" />

        <ImageView
            android:id="@+id/closeView"
            android:layout_width="14dp"
            android:layout_height="14dp"
            android:layout_margin="@dimen/space_24"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:src="@drawable/ic_close"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0"
            app:tint="#a7a7a7" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.core.widget.NestedScrollView
        android:id="@+id/categoryScrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/nested_bottom_sheet_background"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintBottom_toTopOf="@+id/categoryInfoViewGroup"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/interestSheetTitleViewGroup">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/bottom_sheet_background">

            <TextView
                android:id="@+id/titleView"
                style="@style/EstadaoTextView.Bold"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/space_15"
                android:paddingHorizontal="@dimen/space_18"
                android:text="@string/for_you_categories_view_title"
                android:textColor="@color/categories_text"
                android:textSize="@dimen/font_18"
                android:textStyle="bold"
                app:layout_constraintBottom_toTopOf="@+id/messageView"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/messageView"
                style="@style/EstadaoTextView.Regular"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/space_15"
                android:paddingHorizontal="@dimen/space_18"
                android:text="@string/for_you_categories_view_message"
                android:textColor="@color/interest_message"
                android:textSize="@dimen/font_14"
                app:layout_constraintBottom_toTopOf="@+id/expandedFilterView"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/titleView" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/expandedFilterView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/space_20"
                android:background="@color/categories_background"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/messageView" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/categoryInfoViewGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/categories_background"
        android:elevation="@dimen/space_4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/categoryCountViewGroup"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginHorizontal="@dimen/space_12"
            android:layout_marginVertical="@dimen/space_10"
            android:background="@drawable/gray_round_border"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/proceedView"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/categorySelectedCount"
                style="@style/EstadaoTextView.Bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingVertical="@dimen/space_8"
                android:paddingStart="20dp"
                android:paddingEnd="0dp"
                android:text="0"
                android:textColor="@color/categories_text"
                android:textSize="@dimen/font_14"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/totalCategorySelected"
                app:layout_constraintHorizontal_chainStyle="packed"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/totalCategorySelected"
                style="@style/EstadaoTextView.Regular"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingVertical="@dimen/space_8"
                android:paddingStart="0dp"
                android:paddingEnd="20dp"
                android:text="/15"
                android:textColor="#a7a7a7"
                android:textSize="@dimen/font_14"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_chainStyle="packed"
                app:layout_constraintStart_toEndOf="@+id/categorySelectedCount"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>

        <Button
            android:id="@+id/proceedView"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginVertical="@dimen/space_10"
            android:layout_marginEnd="@dimen/space_12"
            android:background="@drawable/bg_button_enable"
            android:drawableEnd="@drawable/ic_check_white"
            android:drawablePadding="2dip"
            android:enabled="false"
            android:gravity="center"
            android:paddingStart="30dp"
            android:paddingEnd="26dp"
            android:singleLine="true"
            android:text="pronto"
            android:textColor="@color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/categoryCountViewGroup"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

这是我希望显示小吃栏的方式。

这是 Snackbar 的显示方式。

我在另外一个fragments里测试了,也是一样的。在另一个项目中,snackbar 正常显示。 有人遇到同样的问题吗?

请尝试将您的 Activity 的父主题替换为:

parent="Theme.AppCompat.DayNight.NoActionBar"

parent="Theme.MaterialComponents.DayNight.NoActionBar"