如何在底部按钮顶部显示小吃栏

问题描述 投票:0回答:1

我有一个带有数据绑定和协调器布局的布局,其中包含底部的按钮,该按钮在向下滚动时隐藏。我想在此屏幕上显示 Snackbar,但如果我显示它,它会显示在按钮上方,但我希望它位于按钮顶部 这是布局:

<layout 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">

    <data>
         ...
    </data>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/mainCoordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        ...


    <FrameLayout
            android:id="@+id/buttons"
            ...
            app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
            app:layout_constraintBottom_toBottomOf="parent">

            <com.google.android.material.button.MaterialButton
                android:id="@+id/button"
                .../>
    </FrameLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

这就是我显示小吃栏的方式:

Snackbar.make(ContextThemeWrapper(binding.root.context), binding.root, message, length).show()

我试图让这个框架布局消失,但小吃栏仍然显示在按钮应该在的区域上方

android snackbar
1个回答
0
投票

您可以为您的小吃栏使用自定义布局。通过在布局中定义底部填充,您可以将其显示在底部按钮的顶部。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/transparent"
    android:paddingBottom="@dimen/bottom_buttons_height">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal"
            android:paddingStart="@dimen/big_padding"
            android:paddingTop="@dimen/default_padding"
            android:paddingEnd="@dimen/big_padding"
            android:paddingBottom="@dimen/default_padding"
            android:background="@android:color/holo_blue_light">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Toast message." />
        </LinearLayout>
</FrameLayout>

然后您可以在小吃栏中使用此布局:

val snackBar = Snackbar.make(
    this.getActivityRootView(),
    "",
    Snackbar.LENGTH_LONG
)
val snackBarLayout: View = LayoutInflater.from(this).inflate(R.layout.your_snackbar_layout, null)
snackBar.view.setPadding(0, 0, 0, 0)
(snackBar.view as ViewGroup).apply {
    removeAllViews()
    addView(snackBarLayout)
    setBackgroundColor(Color.TRANSPARENT)
}
snackBar.show()
© www.soinside.com 2019 - 2024. All rights reserved.