【问题标题】:Anchor FloatingActionButton锚点浮动操作按钮
【发布时间】:2017-02-19 05:21:51
【问题描述】:

我正在使用 Android 支持 FloatingActionButton (FAB) 和 CoordinatorLayout,我希望将 FAB 锚定到 CardView

这是我的代码:

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

<LinearLayout
  ...>
     <android.support.v7.widget.CardView
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    ...
</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="32dp"
    android:src="@android:drawable/ic_menu_send"
    app:layout_anchor="@id/container"
    app:layout_anchorGravity="bottom|end" />

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

这可行,但有时 FAB 会出现在页面的左上角,而不是锚定。

在这种情况下,当我按下 FAB 时,它会回到锚定状态。 CardView 不是 CoordinatorLayout 的直接子代,但我使用的 [Support Library 24.2.1] 允许这样做。

可能是什么问题?

【问题讨论】:

  • 是包含其他视图的线性布局吗?
  • 是的,LinearLayout 包含其他视图

标签: android android-layout android-support-library android-coordinatorlayout floating-action-button


【解决方案1】:

您是否尝试将其锚定到线性布局而不是 CoordinatorLayout?

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

<LinearLayout
  ...>
     <android.support.v7.widget.CardView
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    ...
<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="32dp"
    android:src="@android:drawable/ic_menu_send"
    app:layout_anchor="@id/container"
    app:layout_anchorGravity="bottom|end" />
</LinearLayout>

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

【讨论】:

  • 是的,我尝试在很多地方移动 FAB,但在这种情况下它消失了。
  • 没什么变化,同样的问题
【解决方案2】:

尝试删除以下内容:

app:layout_anchor="@id/container"
app:layout_anchorGravity="bottom|end"

并添加:

android:layout_gravity="bottom|end"

【讨论】:

    【解决方案3】:

    做FAB的LinearLayout锚点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-26
      • 2018-01-01
      • 1970-01-01
      • 2015-06-25
      • 2018-10-18
      • 2015-09-01
      • 1970-01-01
      • 2015-11-14
      相关资源
      最近更新 更多