如何将浮动操作按钮对齐到中心

dev*_*dev 9 android android-xml floating-action-button

我有一个FloatingActionButton.我希望它是2个LinearLayouts的中间和这样的屏幕中心. 在此输入图像描述

目前我的设计是这样的

在此输入图像描述

我希望它在屏幕的正中心.怎么做?

这是我的整个xml代码

<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" />
Run Code Online (Sandbox Code Playgroud)

Int*_*iya 10

CoordinatorLayout是一个FrameLayout,所以试试这种方式,我希望它会对你有所帮助.

 app:layout_anchorGravity="bottom|center"
Run Code Online (Sandbox Code Playgroud)

  • 这应该有效,因为CoordinatorLayout是一个FrameLayout,而不是一个RelativeLayout,其中`layout_centerHorizo​​ntal ="true"`可以工作. (3认同)

Mus*_*sef 5

以上解决方案对我来说不起作用。我将其更改android:layout_gravity

android:layout_gravity="bottom|center"

希望能帮助到你 :-)