How to align Floating Action Button to center

心不动则不痛 提交于 2019-12-23 07:29:43

问题


I have one FloatingActionButton. I want it to be middle of 2 LinearLayouts and center of screen like this.

Currently My design is like this

I want it to be at the exact center of the screen. How to do that?

This is my whole xml code

<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">
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/viewA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.6"
        android:orientation="horizontal"/>
    <LinearLayout
        android:id="@+id/viewB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:orientation="horizontal"/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:src="@drawable/ic_plus"
    app:layout_anchor="@id/viewA"
    android:layout_centerHorizontal="true"
    app:layout_anchorGravity="bottom"
    android:layout_centerInParent="true" />


回答1:


Your CoordinatorLayout is a FrameLayout , So try this way, I hope it will helps you .

 app:layout_anchorGravity="bottom|center"



回答2:


Above solutions did not work for me somehow. I changed the android:layout_gravity to

android:layout_gravity="bottom|center"

Hope it helps :-)




回答3:


Setting layout_centerHorizontal and layout_centerInParent won't work in a CoordinatorLayout. They are only allowed in a RelativeLayout. You should do it like this:

app:layout_anchorGravity="center|bottom"



回答4:


Change to this

app:layout_anchor="@id/viewA"
android:layout_centerHorizontal="true"


来源:https://stackoverflow.com/questions/32117341/how-to-align-floating-action-button-to-center

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!