android - FAB 未使用 RecyclerView 定位在 CoordinatorLayout 内

标签 android android-coordinatorlayout floating-action-button

我正在尝试显示带有 FAB 的项目列表以添加新项目。当前画面如下。

enter image description here

我有一个只有 RecyclerView 和 FAB 的 CoordinatorLayout。我的问题是 FAB 没有锚定到页面底部,而是锚定到 RecyclerView 的底部,它的高度看起来就像它的 wrap_content,但我已经声明为 match_parent 。 RecylerView 具有银色背景以用于说明目的。

<android.support.design.widget.CoordinatorLayout
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"
tools:context="com.ae.apps.tripmeter.fragments.expenses.TripsListFragment">

<android.support.v7.widget.RecyclerView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:background="@color/silver"
    android:layout_margin="@dimen/activity_horizontal_margin"
    app:layoutManager="LinearLayoutManager"
    tools:listitem="@layout/fragment_trip"/>


<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_input_add"
    app:elevation="8dp"/>

我不明白为什么我无法将 FAB 定位在页面底部。

最佳答案

尝试将 RecyclerView 放入“内容”布局文件中,并通过

将其添加到主布局中
<include layout="@layout/content_recyclerview" />

编辑:

如果要驻留在 Activity 的底部,FAB 不应放在 Fragment 中。从 fragment 中移除 FAB 并将其添加到主要 Activity 中。像这样:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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:fitsSystemWindows="true"
    tools:context="com.your_company.your_project">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </android.support.design.widget.AppBarLayout>

    <-- ref. your recycler here -->
    <include layout="@layout/content_with_your_list"/>

    <-- Put the FAB here -->
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_input_add"
    />

</android.support.design.widget.CoordinatorLayout>

现在在您的 fragment 中删除 FAB!它应该与 CoordinateLayout 一起驻留在您的主要 Activity 中。在 fragment 中使用简单的 LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_behavior="@string/appbar_scrolling_view_behavior"
              tools:showIn="@layout/activity_your_main_activity"
              >


    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/silver"
        android:layout_margin="@dimen/activity_horizontal_margin"
        app:layoutManager="LinearLayoutManager"/>


</LinearLayout>

您需要进行适当的更改以适合您的项目。


切换 FAB 可见性

为了切换 FAB 的可见性,将以下属性添加到父 Activity 中的 FloatingActionButton

android:visibility="gone"

并使其在代码中可见:

mFab.setVisibility(View.VISIBLE);

或不可见,再次使用

mFab.setVisibility(View.GONE);

只要你需要,例如。当显示或删除 fragment 时。

关于android - FAB 未使用 RecyclerView 定位在 CoordinatorLayout 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44812843/

相关文章:

android - 如何在android中获取所有 View fragment ?

android - 如何在不同 cpps 中创建的节点之间进行交互...?

android - 如何以编程方式打开和关闭 layout_behavior 行为?

android - com.admob.android.ads.AdView 未绑定(bind)前缀?

android - 找不到处理 Intent 的 Activity - 错误

android - @string/appbar_scrolling_view_behavior 在 com.android.support :design 中被标记为私有(private)

Android 全屏阻止 View ,如 Google Calendar FAB

android - 如何将渐变可绘制设置为 FloatingActionButton 上的 backgroundTint 属性

android - AppBarLayout 显示意外行为?如何在 CollapsibleToolBarLayout 中禁用应用程序标题?

android - 协调器布局不适用于 Listview