【问题标题】:Floating Action Button is not displaying - viewpager浮动操作按钮未显示 - viewpager
【发布时间】:2016-11-09 22:49:13
【问题描述】:

我在这里阅读了一些关于这个问题的帖子,但我无法显示 FAB。我尝试了不同的方法,但我真的不知道发生了什么。

<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="112dp"
    android:background="@color/colorPrimary"
    android:elevation="@dimen/elevation_toolbar"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:contentScrim="@color/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|enterAlways"
        android:fitsSystemWindows="true">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:layout_marginLeft="@dimen/list_toolbar_side_margin"
            android:layout_marginRight="@dimen/list_toolbar_side_margin"
            app:layout_collapseMode="pin" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            style="@style/AppTabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:tabGravity="fill"
            app:tabMode="fixed"
            app:tabTextAppearance="@style/AppTabTextAppearance"/>

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

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

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_id"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_gravity="end|bottom"
    android:layout_margin="16dp"
    android:clickable="true"
    android:src="@drawable/ic_add"
    />

如果它检查了 android studio 中的预览,那么 fab 就在那里。甚至,当我调试应用程序时,我可以看到工厂已创建。

我也尝试使用带有 **app:layout_anchorGravity= 的 app:layout_anchor="@id/viewpager"app:layout_anchor="@id/app_bar_layout" "bottom|right" ** 它没有用。 FAB 被 viewpager 覆盖。我怎样才能避免这种情况?

我附上主要活动的图片。您将看到一个“添加植物”按钮。此按钮应替换为 FAB。

【问题讨论】:

  • 你想要你的工厂在右下角吗?
  • 是的。我需要住在浏览器上的工厂。激活哪个 viewpager 的片段并不重要。
  • 你修好了吗?我也有同样的问题

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


【解决方案1】:

在你的 xml 中试试这个

app:layout_anchor="@id/viewpager"
app:layout_anchorGravity="bottom|right"

并删除

android:layout_gravity="end|bottom"

【讨论】:

  • 我使用了 app:layout_anchorGravity="bottom|right" 但没有用。我忘了在我的问题中添加这个。我会更新id。
  • 这个解决方案不起作用,用 Android4 测试过
【解决方案2】:

尝试将 floatingActionButton 插入 frameLayout 并将此布局插入到您的主布局中。它对我有用

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   .
   .
   .

   <FrameLayout
       android:id="@+id/rootLayout"
       android:layout_width="match_parent"
       android:layout_height="match_parent">

       <android.support.design.widget.FloatingActionButton
           android:id="@+id/fab_add_room"
           android:layout_width="wrap_content"
           android:layout_marginBottom="8dp"
           android:layout_marginRight="8dp"
           android:layout_height="wrap_content"
           android:layout_gravity="bottom|right"
           android:src="@drawable/ic_add"
           app:fabSize="normal" />

  </FrameLayout>
  .
  .
  .
</android.support.constraint.ConstraintLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-25
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    相关资源
    最近更新 更多