Anchor FloatingActionButton

I am using Android support FloatingActionButton(FAB) and CoordinatorLayout, and I want to bind FAB to CardView.

This is my code:

<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>

This works, but sometimes FAB appears on the left side of the page, rather than at anchor.

In this case, when I press FAB, it returns to the anchor. CardViewis not a direct child CoordinatorLayout, but the [Support Library 24.2.1] that I use allows this.

What could be the problem?

+4
source share
3 answers

Try removing the following:

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

and add:

android:layout_gravity="bottom|end"
+2
source

Linear Layout, 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>
0

Bind the LinearLayout FAB.

0
source

Source: https://habr.com/ru/post/1657308/


All Articles