Android 显示小吃店时向上移动按钮

Android moving buttons upwards when showing snack bar

我想在显示小吃店时向上移动我的按钮,这样它就不会隐藏它们。我已经尝试过堆栈溢出的不同解决方案,但我无法找到解决方案。这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:paddingLeft="0dp"
    android:paddingRight="0dp" >

    <include
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        layout="@layout/fragment_expiry_dates_list_header" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/products_list"
        android:name="com.example.expirydate.ui.main.ExpiryDatesFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/header"
        android:layout_above="@id/buttonNewOrOpened"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginBottom="3dp"
        app:layoutManager="LinearLayoutManager"
        tools:context=".expirydatefragment.ExpiryDatesFragment"
        tools:listitem="@layout/fragment_expiry_dates_list_item" />

    <Button
        android:id="@+id/buttonNewOrOpened"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="false"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:enabled="false"
        android:text="New" />

    <Button
        android:id="@+id/buttonUsed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toEndOf="@id/buttonNewOrOpened"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:enabled="false"
        android:text="Used" />

    <ImageButton
        android:id="@+id/buttonScanner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="center"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:layout_toStartOf="@id/buttonAdd"
        android:background="@drawable/roundcorner"
        android:padding="8dp"
        android:src="@android:drawable/ic_menu_gallery"
        android:contentDescription="scan product" />

    <ImageButton
        android:id="@+id/buttonAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="center"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:padding="8dp"
        android:background="@drawable/roundcorner"
        android:src="@android:drawable/ic_input_add"
        android:contentDescription="add product" />

</RelativeLayout>

我想做的是移动小吃店上方底部的那 4 个按钮。显示小吃店的代码:

Snackbar snackbar = Snackbar.make(getActivity().findViewById(android.R.id.content), "msg", Snackbar.LENGTH_SHORT);
snackbar.setAction("Undo", v -> {});
snackbar.show();

目前看起来是这样的(底部隐藏了4个按钮):

我已经尝试了多种解决方案,包括添加我自己的 layout_behavior 或添加 app:insetEdges="bottom",但没有任何效果。

尝试使用 app:insetEdges="bottom" 时,您的方法几乎是正确的。但是首先 insetEdgeslayout_dodgeInsetEdges 只能在 CoordinatorLayout 上正常工作。您需要将 RelativeLayout 更改为 CoordinatorLayout。之后将您的按钮分组在一个布局中,例如它可以是 RelativeLayout 并向其添加 app:layout_dodgeInsetEdges="bottom"android:layout_gravity="bottom"。由于默认情况下 Snackbar 具有 app:insetEdges="bottom",因此您只需将该属性设置为按钮容器即可。

为了使 CoordinatorLayout 正常工作,还应该将您的 header 包裹在 AppBarLayout 中并将 app:layout_behavior="@string/appbar_scrolling_view_behavior" 添加到 RecyclerView。在这里我可以提供这个解决方案的模板。

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    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" >

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- Your header here or your header need to be already wrapped in AppBarLayout -->

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/products_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="LinearLayoutManager"
        tools:context=".expirydatefragment.ExpiryDatesFragment"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <RelativeLayout
        android:id="@+id/buttonsContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_dodgeInsetEdges="bottom"
        android:layout_gravity="bottom">

        <!--Your Buttons here-->

    </RelativeLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>