【问题标题】:How to align fab to the bottom right of screen inside a fragment in a recycler view?如何在回收站视图中将 fab 与片段内的屏幕右下角对齐?
【发布时间】:2021-11-11 09:38:04
【问题描述】:

我有“activity_main”,其中使用了折叠工具栏和片段容器。在容器内部,我添加和替换了多个片段,其中一个是“activity_user_shops_list”,它是一个回收站视图。我想将 fab 与此列表片段内的屏幕右下角对齐。我找不到对齐的方法。任何帮助表示赞赏。

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:background="#F3F3F3">


    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/id_appbar_layout_m"
        android:layout_width="match_parent"
        android:layout_height="200dp">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/id_collapsing_toolbar_m"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|snap|exitUntilCollapsed"
            app:titleCollapseMode="scale"
            app:title="@string/user_name"
            app:expandedTitleGravity="center_horizontal"
            app:expandedTitleMarginTop="160dp"
            android:background="#F3F3F3">


            <com.google.android.material.imageview.ShapeableImageView
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:src="@mipmap/ic_launcher"
                app:shapeAppearanceOverlay="@style/circleImageView"
                android:layout_gravity="center"/>


            <com.google.android.material.appbar.MaterialToolbar
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"/>


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


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

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/id_appbar_layout_m"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/id_fragment_container_m"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"/>

    </androidx.core.widget.NestedScrollView>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

fragment_user_shops_list.xml:

<?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"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/null_gray">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/id_recycler_view_e_shops"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/id_floating_button_e_shops"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/add_a_shop"
        android:src="@drawable/ic_plus"
        app:tint="@color/white"
        app:maxImageSize="30dp"
        app:useCompatPadding="true"/>

</RelativeLayout>

编辑:

在添加android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" 并让所有fab 父母match_parent 之后,它仍然无法正常工作。

不管我做什么结果都是一样的。结果如下(灰色区域为“fragment_user_shops_list”):

【问题讨论】:

  • 试试这个问题的答案:stackoverflow.com/questions/30769080/…
  • 不幸的是它没有解决问题。我看到的主要问题是“fragment_user_shops_list”与它的父级不匹配,即使我将高度更改为 match_parent。
  • 尝试将父级也匹配到 RecyclerView
  • 我试过了,但是晶圆厂低于屏幕底部。我认为没有办法做到这一点。

标签: android android-recyclerview floating-action-button


【解决方案1】:

您需要将android:layout_alignParentRightandroid:layout_alignParentBottom 设置为您的FloatingActionButton 以与右下角对齐

这是更新后的 xml 文件的样子:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/id_recycler_view_e_shops"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/id_floating_button_e_shops"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_margin="16dp"
        android:contentDescription="Shop"
        android:src="@android:drawable/btn_plus"
        app:maxImageSize="30dp"
        app:tint="@android:color/white"
        app:useCompatPadding="true" />

</RelativeLayout>

【讨论】:

  • 我已经更新了答案。
  • 您需要设置 'FragmentContainerView' 高度以匹配父级也占用可用的全部空间
  • 是的,正如我在编辑中提到的,我将 fab 的所有父级(包括嵌套滚动视图和片段容器的宽度和高度)更改为 match_parent,但它仍然无法正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-24
  • 2017-12-18
  • 2017-02-01
  • 2020-07-07
  • 1970-01-01
  • 2020-02-21
  • 2013-05-20
相关资源
最近更新 更多