Android 如何在两个编辑文本之间对齐FAB

Android 如何在两个编辑文本之间对齐FAB,android,android-layout,alignment,Android,Android Layout,Alignment,我想将浮动操作按钮放置在CardView中两个EditText之间,如下所示: 我尝试了以下方法,但不起作用 我该怎么做?任何帮助都将不胜感激 <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" xmlns:app="ht

我想将浮动操作按钮放置在CardView中两个EditText之间,如下所示:

我尝试了以下方法,但不起作用

我该怎么做?任何帮助都将不胜感激

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/tools"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    card_view:cardCornerRadius="25dp"
    android:layout_height="wrap_content"
    android:layout_margin="8dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"

        >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <EditText
                android:id="@+id/etOrigin"
                android:layout_width="204dp"
                android:layout_height="wrap_content"
                android:hint="Enter origin address" />

        </LinearLayout>
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/btnFindPath"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_marginBottom="16dp"
            android:layout_marginRight="16dp"
            android:onClick="fabClick"
            android:src="@mipmap/ic_add_white_24dp"
            app:fabSize="normal"
            app:layout_anchor="@id/fab_coordinator_layout"
            app:layout_anchorGravity="left|end" />


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <EditText
                android:id="@+id/etDestination"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="Enter destination address" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >


        </LinearLayout>


    </LinearLayout>
</android.support.v7.widget.CardView>

您可以这样做-

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/tools"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    card_view:cardCornerRadius="25dp"
    android:layout_height="wrap_content"
    android:layout_margin="8dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <EditText
                android:id="@+id/etOrigin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Enter origin address" />
            <EditText
                android:id="@+id/etDestination"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="Enter destination address" />
        </LinearLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/btnFindPath"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:onClick="fabClick"
            android:src="@drawable/ic_add"
            app:fabSize="normal"
            android:layout_gravity="center_vertical"
            app:layout_anchor="@id/fab_coordinator_layout" />

    </LinearLayout>
</android.support.v7.widget.CardView>


我已更改了填充和边距,请根据您的需要进行设置。

使用
RelativeLayout
如下:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
card_view:cardCornerRadius="5dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/inputHolder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/fabHolder"
        android:layout_toStartOf="@+id/fabHolder"
        android:orientation="vertical">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/fabHolder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/inputHolder"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/inputHolder">

        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:src="@android:drawable/ic_menu_send" />
    </RelativeLayout>

</RelativeLayout>


尝试使用线性布局

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    card_view:cardCornerRadius="25dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Your hint" />

            <EditText
                android:id="@+id/etDestination"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="Your hint" />
        </LinearLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/btnFindPath"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="fabClick"
            android:src="@drawable/ic_down"
            app:fabSize="normal"
            android:layout_gravity="center"
            app:layout_anchor="@id/fab_coordinator_layout" />

    </LinearLayout>
</android.support.v7.widget.CardView>


使用相对布局或约束布局尽可能避免使用相对布局,更不用说嵌套的相对布局了。请看下面的解释-是的,但对初学者来说很容易。但在这种情况下,嵌套线性布局比嵌套相对布局简单。看看我的答案。