android – 使用TabLayout和ViewPager时出现在屏幕下的FloatingActionButton

看看下面的FAB:

《android – 使用TabLayout和ViewPager时出现在屏幕下的FloatingActionButton》

除非我折叠工具栏,否则它不会出现.这是我的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设置任何行为,为什么会发生这种情况?是否有任何属性我应该添加到任何东西,所以我可以防止这种行为?

最佳答案 只需从每个片段中删除您的FloatingActionButton并将其添加到您的Activity中.这样不仅FAB保持不变,而且还可以解决您的问题.然后你可以在Fragment中使用getActivity().findViewByIf(id)并在每个Fragment中设置一个onClickListener.

点赞