Given I'm using a layout like this:
<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
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/appbar"
android:layout_width="match_parent"
android:layout_height="@dimen/flexible_space_image_height"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:statusBarScrim="@android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/mainView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
<android.support.design.widget.FloatingActionButton
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginBottom="20dp"
app:fabSize="normal"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="bottom|center_horizontal"
/>
</android.support.design.widget.CoordinatorLayout>
Which is pretty much the standard Cheesesquare sample - except the FloatingActionButton, which I would like to move up by about 20dp.
However, this will not work. No matter if I use margin, padding etc - the button will always be centered at the edge of the anchor, like this:
How can I move the FAB up by 20dp as intended?
Easy workaround is to anchor a random layout to where FAB was anchored, give it specific margin, and then anchor FAB to random layout, like this
I was able to get around this issue by using both
layout_anchor
layout_anchorGravity
along with some padding.. The anchor attribute allows you to have aView
position itself relative to another view. However, it doesn't exactly work in the same way thatRelativeLayout
does. More on that to follow.layout_anchor
specifies whichView
your desiredView
should be positioned (i.e., thisView
should be placed relative to theView
specified by thelayout_anchor
attribute). Then,layout_anchorGravity
specifies which side of the relativeView
the currentView
will be positioned, using the typical Gravity values (top
,bottom
,center_horizontal
, etc.).The issue with using just these two attributes alone is that the center of the
View
with the anchors will be placed relative to the otherView
. For example, if you specify aFloatingActionButton
to be anchored to the bottom of aTextView
, what really ends up happening is that the the center of the FAB is placed along the bottom edge of theTextView
.To get around this issue, I applied some padding to the FAB, enough such that the top edge of the FAB was touching the bottom edge of the
TextView
:You might have to increase the padding to get the desired effect. Hope that helps!
Try putting it in a linear layout that have padding:
To anchor the![Fab as anchor to the AppBar](https://i.stack.imgur.com/WyTMN.png)
FloatingActionButton
below theAppBar
like this:Extend the
FloatingActionButton
and overrideoffsetTopAndBottom
:As there might be bugs in the design-support-lib concerning the
CoordinatorLayout
& margins, I wrote aFrameLayout
that implements/copies the same "Behavior" like the FAB and allows to set apadding
to simulate the effect:Be sure to put it in the
android.support.design.widget
package as it needs to access some package-scoped classes.