【问题标题】:FloatingActionButton appearing under screen when using TabLayout and ViewPager使用 TabLayout 和 ViewPager 时出现在屏幕下方的 FloatingActionButton
【发布时间】:2017-01-10 19:20:38
【问题描述】:

看看下面的FAB

除非我折叠 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 没用
  • 曾经遇到过完全相同的问题,请提供您的整个布局代码。没有它解决这个问题是不可行的。

标签: android android-viewpager android-tablayout android-coordinatorlayout floating-action-button


【解决方案1】:

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

【讨论】:

【解决方案2】:

协调器布局不像相对布局,你不能有多个孩子而不互相干扰。所以只需创建一个协调器布局的孩子,即相对布局,并在其中添加 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>

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-01
      • 2015-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多