Android:在相对布局中将控件和另一个控件的中心对齐

Android:在相对布局中将控件和另一个控件的中心对齐,android,android-layout,Android,Android Layout,我已经删除了我的旧问题,并用更多信息重新发布了我先前的问题,因为原来的问题被严重误判 以下是原始布局。右边是一个按钮,两个编辑文本框占据剩余宽度的一半 实际上,这是嵌套在另一个线性布局中的,但这是我需要帮助的相关部分 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_

我已经删除了我的旧问题,并用更多信息重新发布了我先前的问题,因为原来的问题被严重误判

以下是原始布局。右边是一个按钮,两个编辑文本框占据剩余宽度的一半

实际上,这是嵌套在另一个线性布局中的,但这是我需要帮助的相关部分

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp" >

        <EditText
            android:id="@+id/txtUser"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:layout_weight="1"
            android:maxLines="1" />

        <EditText
            android:id="@+id/txtPassword"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:layout_weight="1"
            android:maxLines="1" />

        <Button
            android:id="@+id/btnAddPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/add" />

    </LinearLayout>


        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/LinInput" />

        <TextView
            android:id="@android:id/empty"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/LinInput"
            android:text="@string/EmptyPass"
            android:textSize="24sp"
            android:padding="10dp" />

</RelativeLayout>

我的目标是减少文件中建议的线性布局的数量。一种可能是有一个空视图,正如我在下面的布局中所做的那样。空视图的宽度等于剩余宽度(减去按钮)。我已在此视图的任一侧设置了两个EditText控件

现在,我只需要弄清楚如何使宽度从视图的中间开始

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/vwEmptyView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_toLeftOf="@+id/btnAddPassword"
        android:layout_centerHorizontal="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">

    </View>

    <EditText
        android:id="@+id/txtUser"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_weight="1"
        android:maxLines="1"
        android:layout_toLeftOf="@+id/vwEmptyView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:id="@+id/txtPassword"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_weight="1"
        android:maxLines="1"
        android:layout_toRightOf="@+id/vwEmptyView"
        android:layout_toLeftOf="@+id/btnAddPassword"
        android:layout_toStartOf="@+id/btnAddPassword" />

    <Button
        android:id="@+id/btnAddPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnAddPassword" />

</RelativeLayout>

这是渲染时的外观。


顺便说一句,Hierarchy Viewer为另一个相对布局显示了三个绿点,但这一个显示为红色、红色和黄色。

这就是使用
ConstraintLayout实现所需布局的方法

<android.support.constraint.ConstraintLayout
    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">

    <EditText
        android:id="@+id/txtUser"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:layout_weight="1"
        android:ems="10"
        android:maxLines="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/txtPassword"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/txtPassword"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:layout_toStartOf="@+id/btnAddPassword"
        android:layout_weight="1"
        android:ems="10"
        android:maxLines="1"
        app:layout_constraintLeft_toRightOf="@+id/txtUser"
        app:layout_constraintRight_toLeftOf="@+id/btnAddPassword"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnAddPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="add"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginEnd="8dp" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_below="@+id/btnAddPassword"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/btnAddPassword"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>


正如您可能已经注意到的,您不需要任何其他视图来将2
editText
居中放置在可用空间中。我知道问题在于如何使用
RelativeLayout
进行同样的操作,但我想不出任何方法来做到这一点,而不添加一些不必要的视图。在这种情况下,使用
ConstraintLayout
是最好的解决方案。

您的第一种方法完全可以。您不需要删除
线性布局
。没有那么多需要避免的。如果没有线性布局,您尝试的任何内容都很难实现