当使用TabLayout和ViewPager时,FloatingActionButton出现在屏幕下方

4

看一下下面的FAB

Image

除非我折叠Toolbar,否则它不会出现。这是我的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="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/toolbar"
    android:layout_marginTop="0dp"
    android:animateLayoutChanges="true"
    android:background="@android:color/white"
    android:layoutDirection="locale"
    android:orientation="vertical"
    android:padding="0dp">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/cLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="0dp" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@color/accent"
            android:src="@drawable/ic_add"
            app:layout_anchor="@id/list"
            app:layout_anchorGravity="bottom|end" />
    </android.support.design.widget.CoordinatorLayout>
</RelativeLayout>

如果我没有为FAB设置任何行为,为什么会发生这种情况?有没有任何属性可以添加到任何地方,以便我可以防止这种行为?


从相对布局的顶部中删除此行:android:layout_below="@+id/toolbar" - cafebabe1991
将协调布局设置为最顶层布局。 - cafebabe1991
@cafebabe1991 没有起作用。 - Ali Bdeir
我曾经遇到过完全相同的问题,提供你整个布局代码。如果没有它,解决这个问题将不可行。 - Darshan Miskin
3个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
3

只需将您的FloatingActionButton从每个 Fragment中移除并添加到您的Activity中。这样不仅可以让FAB保持在原地,而且还可以解决您的问题。然后,您可以在每个Fragment中使用getActivity().findViewByIf(id),并在每个Fragment中设置一个onClickListener


点击事件的问题。如何找到当前可见的片段? - Mitesh Vanaliya
您可以尝试以下方法:
  1. https://dev59.com/I2Ml5IYBdhLWcg3wMUiP
  2. https://pupli.net/2017/06/05/get-current-fragment-displayed-in-viewpager-on-android/
- Burhanuddin Rashid

0
我建议您删除RelativeLayout并像这样重新构建您的布局:
<android.support.design.widget.CoordinatorLayout
android:id="@+id/cLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="0dp" />
<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@color/accent"
    android:src="@drawable/ic_add"
    app:layout_anchor="@id/list"
    app:layout_anchorGravity="bottom|end" />
<com.rey.material.widget.ProgressView
    android:id="@+id/progress_bar"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    app:pv_autostart="true"
    app:pv_circular="true"
    app:pv_progressMode="indeterminate"
    app:pv_progressStyle="@style/Material.Drawable.CircularProgress" />
</android.support.design.widget.CoordinatorLayout>

0

协调布局与相对布局的行为不同,您不能在没有相互干扰的情况下拥有多个子项。因此,只需将协调布局的一个子项即相对布局,并在其中添加recyclerView和floatingActionButton。

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"> 

          <android.support.v7.widget.RecyclerView
              android:id="@+id/list"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:layout_marginTop="0dp" />

                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/fab"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:background="@color/accent"
                    android:src="@drawable/ic_add"
                    app:layout_anchor="@id/list"
                    app:layout_anchorGravity="bottom|end" /></RelativeLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,