android - 在 Android 中显示 FAB 上方的 snackbar

标签 android kotlin floating-action-button android-snackbar material-components-android

尝试使用 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 应该显示在底部应用栏和 fab 上方,但当我尝试显示带有底部边距的 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 和底部应用栏上方显示 snackbar ? 对不起我的英语!

最佳答案

使用Snackbar在 Material 组件库中并使用 setAnchorView方法使 Snackbar 出现在特定 View 上方。

根据您的情况,您可以使用:

FloatingActionButton fab = findViewById(R.id.photoFab);
Snackbar snackbar = Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG);
snackbar.setAnchorView(fab);

关于android - 在 Android 中显示 FAB 上方的 snackbar ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52302340/

相关文章:

android - 如何使用支持库 23 修复缩放的 FloatingActionButton 上的阴影

android - 是否可以在不同的 fragment 中为 float 操作按钮设置不同的操作?

android - 如何让 FAB 在 ViewPager 中不被移动,而成为其中的 fragment 的一部分?

android - 插入耳机后重新路由音频

android - 如何制作android凸起按钮?

android - 5.1 崩溃 - TaskDescription 的原色应该是不透明的

java - Dagger-Hilt 你忘记应用 Gradle 插件了吗?但我应用了插件

android - Android 11 中未显示权限对话框

android - 连接重置后 Realm PermissionManager.getPermissions() 错误

java - ActiveMQ:拦截发布到某个主题