layout_constraintHorizontal_weight = 1 在 ConstraintLayout 不工作

layout_constraintHorizontal_weight = 1 in ConstraintLayout is not Working

TextInputLayout 中的 TextInputEditText 使用 wight 属性 等于 ConstraintLayout 中的宽度。使用 layout_constraintHorizontal_weight = 1 不是 work.how 使用权重 属性 为 Textinputlayout 提供相同的宽度。这是给出的结果。

我想要以下结果:

xml 代码是:

<androidx.constraintlayout.widget.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">


    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/tilFirstName1"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"


        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="32dp"

        app:layout_constraintHorizontal_weight="1"
        app:boxBackgroundMode="outline"
        app:endIconMode="clear_text"
        app:hintEnabled="false"
        app:layout_constraintEnd_toStartOf="@+id/tilLastName1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/etFirstName1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:background="@drawable/custom_edittext_input"
            android:hint="First Name"
            android:inputType="textPersonName" />
    </com.google.android.material.textfield.TextInputLayout>



    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/tilLastName1"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="32dp"

        app:boxBackgroundMode="outline"
        app:endIconMode="clear_text"
        app:hintEnabled="false"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/etLastName1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/custom_edittext_input"
            android:hint="Last Name"
            android:inputType="textPersonName" />
    </com.google.android.material.textfield.TextInputLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

我对您的代码进行了一些修改以达到此目的。技巧是将第一个 firstName1 的结尾限制在 lastName1 的开头,并对 lastName1 执行相反的操作,并在边界处分配它们之间的边距。

<androidx.constraintlayout.widget.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">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/tilFirstName1"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="8dp"
        app:boxBackgroundMode="outline"
        app:endIconMode="clear_text"
        app:hintEnabled="false"
        app:layout_constraintEnd_toStartOf="@+id/tilLastName1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/etFirstName1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/custom_edittext_input"
            android:hint="First Name"
            android:inputType="textPersonName" />
    </com.google.android.material.textfield.TextInputLayout>


    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/tilLastName1"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="8dp"
        app:boxBackgroundMode="outline"
        app:endIconMode="clear_text"
        app:hintEnabled="false"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/tilFirstName1"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/etLastName1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/custom_edittext_input"
            android:hint="Last Name"
            android:inputType="textPersonName" />
    </com.google.android.material.textfield.TextInputLayout>

</androidx.constraintlayout.widget.ConstraintLayout>