首页 > 解决方案 > 显示小吃店时Android向上移动按钮

问题描述

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

<?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",但没有任何效果。

标签: androidandroid-relativelayoutandroid-snackbar

解决方案


尝试使用app:insetEdges="bottom". 但是首先insetEdges并且layout_dodgeInsetEdges只能与CoordinatorLayout. 您需要将您的更改RelativeLayoutCoordinatorLayout. 之后将您的按钮分组到一个布局中,例如,它可以添加和RelativeLayout添加到它。由于 Snackbar 默认情况下,您只需将该属性设置为按钮容器。app:layout_dodgeInsetEdges="bottom"android:layout_gravity="bottom"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>

推荐阅读