【问题标题】:Android moving buttons upwards when showing snack bar显示小吃店时Android向上移动按钮
【发布时间】:2020-04-08 19:35:43
【问题描述】:

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

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

【问题讨论】:

    标签: android android-relativelayout android-snackbar


    【解决方案1】:

    当您尝试使用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>
    

    【讨论】:

    • 我应该补充一点,我提供的布局是针对属于此布局一部分的片段:pastebin.com/3hsS6X2a 所以 appbar 已经包含在那里。顺便说一句,为什么我需要将 appbar_scrolling_view_behavior 添加到回收站视图?我只想向上移动底部的按钮。
    • 如果不给RecyclerView添加appbar_scrolling_view_behavior,会和AppBarLayout重叠。
    • 所以我尝试过这样的事情:pastebin.com/V8ZaGYJe 但它不起作用,我不知道为什么:( 我什至尝试在底部添加这个测试文本视图,但它显示小吃店时不会上升
    • 您将什么视图作为第一个参数传递给 Snackbar make 方法?我看到您使用 R.id.content 但找不到具有该 ID 的任何视图。将该 id 设置为任何 CoordinatorLayout 子项,然后重试。
    • 是的,这似乎是问题所在!我已将其更改为 getActivity().findViewById(R.id.expiry_date_layout),并将相对布局 id 设置为 expiry_date_layout。
    猜你喜欢
    • 2021-08-03
    • 2015-11-15
    • 2020-05-18
    • 2016-04-01
    • 2021-04-16
    • 1970-01-01
    • 2015-03-10
    • 2018-05-04
    • 2022-12-17
    相关资源
    最近更新 更多