在android中显示FAB上方的snackbar

问题描述 投票:3回答:2

尝试使用androidx创建应用程序。我的布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.photo.PhotoFragment">


    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerPhoto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_anchorGravity="top|center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:itemCount="48"
        tools:layoutManager="GridLayoutManager"
        tools:listitem="@layout/recycler_photo_item"
        tools:spanCount="3" />

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/photoCoordinator"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        >

        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/mainBottomAppBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:fabAlignmentMode="center"
            android:backgroundTint="@color/colorPrimary"
            app:fabCradleMargin="@dimen/cradle_margin"
            app:fabCradleRoundedCornerRadius="@dimen/corner_radius"
            app:hideOnScroll="true"
            app:navigationIcon="@drawable/ic_menu_24px" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/photoFab"
            style="@style/Widget.MaterialComponents.FloatingActionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_camera_alt_24px"
            app:layout_anchor="@id/mainBottomAppBar" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

它看起来像enter image description here当我显示snackbar它看起来像这个enter image description here谷歌说,snackbar应该显示在底部的应用程序栏和晶圆厂,但我不能尝试显示底部边缘的小吃店

val snackbarView = snackbar.view
    val params = snackbarView.layoutParams as CoordinatorLayout.LayoutParams

    params.setMargins(
            params.leftMargin + marginSide,
            params.topMargin,
            params.rightMargin + marginSide,
            params.bottomMargin + marginBottom
    )

    snackbarView.layoutParams = params

fab up to!我如何在fab和底部应用栏上方显示snackbar?对不起我的英语!

android kotlin floating-action-button snackbar
2个回答
2
投票

因此,您遇到的问题是Google仍未更新FAB行为以与新设计保持一致。由于您的FAB生活在协调器布局中并且您正在使用它来启动您的零食栏,因此FAB向上移动以适应(旧行为)

一些解决方案:

  1. 将FAB移出协调器布局,并将其覆盖在底部应用栏及其父协调器布局的顶部 这可能会破坏FAB与底部应用栏的任何互动。这可能不是一个好的长期解决方案,因为将FAB放在协调员中通常是一个好主意
  2. 删除FAB行为 这将删除FAB正在执行的任何行为。与从协调器中删除它相同的缺点,除了它更容易反向。出于这个原因,我更喜欢这种解决方案 您可以使用XML在XML中执行此操作: app.layout_behavior="" 或者使用以下代码在代码中执行: CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) yourView.getLayoutParams(); params.setBehavior(new AppBarLayout.ScrollingViewBehavior()); yourView.requestLayout();

看起来谷歌仍在更新行为来源。完成后,您可能希望删除它,以便它可以使用其默认行为:

https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/floatingactionbutton/FloatingActionButton.java


2
投票

似乎谷歌更新了指南,但没有更新SDK,也没有更新自己的应用程序(如Gmail)。

我在这里报道了这个:

https://issuetracker.google.com/issues/122163315

© www.soinside.com 2019 - 2024. All rights reserved.