提问者:小点点

使用表格布局和查看页面时,浮动操作按钮出现在屏幕下方


看看下面的FAB

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


共3个答案

匿名用户

只需从每个< code >片段中移除您的< code > floating action button ,并将其添加到您的< code>Activity中。这样,不仅晶圆厂留在原地,而且也解决了你的问题。然后可以使用< code>getActivity()。并在每个< code >片段中设置一个< code>onClickListener。

匿名用户

我建议您删除您的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>

匿名用户

协调器布局的行为与相对布局不同,您不能在不干扰彼此的情况下拥有多个子项。所以只需创建一个子协调器布局,即相对布局,并在其中添加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>