EditText 宽度大于其父 TableRow

EditText width is larger than its parent TableRow

我有几个 TableRow,每个里面都有一个 TextView 和一个 EditText。但是 EditText 不适合 table 行,即使我已将其宽度设置为 match_parent。 EditText 在 TableRow 之外结束。我希望它适合 TableRow 宽度。

这是我的意思的图片:

我怎样才能让 EditText 在这里结束,红色的地方?:

这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="32dp"
    tools:context=".activity.ConfigScreenActivity">

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:stretchColumns="*">

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/novo_usuario_TextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="@string/novo_usuario_texto"
                    android:textSize="20sp" />

                <EditText
                    android:id="@+id/novo_usuario_EditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_margin="5dp"
                    android:ems="10"
                    android:hint="Novo Usuário"
                    android:inputType="textPersonName"
                    android:textColorHint="@color/light_gray"
                    android:textSize="20sp"
                    tools:ignore="TextContrastCheck" />

            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/nova_senha_TextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="@string/nova_senha_texto"
                    android:textSize="20sp" />

                <EditText
                    android:id="@+id/nova_senha_EditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_margin="5dp"
                    android:ems="10"
                    android:hint="Nova Senha"
                    android:inputType="textPassword"
                    android:textColorHint="@color/light_gray"
                    android:textSize="20sp"
                    tools:ignore="SpeakableTextPresentCheck,DuplicateSpeakableTextCheck,TextContrastCheck" />
            </TableRow>

        </TableLayout>

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="100dp">

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/confirmar_senha_TextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:singleLine="false"
                    android:text="@string/confirmar_senha_texto"
                    android:textSize="20sp" />

                <EditText
                    android:id="@+id/confirmar_senha_EditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_margin="5dp"
                    android:ems="10"
                    android:hint="Nova Senha"
                    android:inputType="textPersonName"
                    android:textColorHint="@color/light_gray"
                    android:textSize="20sp" />

            </TableRow>

        </TableLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="100dp">

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="20dp"
                android:layout_weight="1"
                android:text="@string/confirmar_button" />

            <Button
                android:id="@+id/cancelar_Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="changeToMainScreen"
                android:layout_marginLeft="20dp"
                android:layout_weight="1"
                android:text="@string/cancelar_button" />
        </LinearLayout>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

您可以通过为 EditText 分配权重来解决此问题

试试看

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="32dp"
    android:orientation="vertical"
    tools:context=".activity.ConfigScreenActivity">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:stretchColumns="*">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/novo_usuario_TextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="@string/novo_usuario_texto"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/novo_usuario_EditText"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:ems="10"
                android:hint="Novo Usuário"
                android:inputType="textPersonName"
                android:textColorHint="@color/light_gray"
                android:textSize="20sp"
                tools:ignore="TextContrastCheck" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/nova_senha_TextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="@string/nova_senha_texto"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/nova_senha_EditText"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:ems="10"
                android:hint="Nova Senha"
                android:inputType="textPassword"
                android:textColorHint="@color/light_gray"
                android:textSize="20sp"
                tools:ignore="SpeakableTextPresentCheck,DuplicateSpeakableTextCheck,TextContrastCheck" />
        </TableRow>

    </TableLayout>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="100dp">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/confirmar_senha_TextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:singleLine="false"
                android:text="@string/confirmar_senha_texto"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/confirmar_senha_EditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_margin="5dp"
                android:ems="10"
                android:hint="Nova Senha"
                android:inputType="textPersonName"
                android:textColorHint="@color/light_gray"
                android:textSize="20sp" />

        </TableRow>

    </TableLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="100dp">

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:layout_weight="1"
            android:text="@string/confirmar_button" />

        <Button
            android:id="@+id/cancelar_Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_weight="1"
            android:onClick="changeToMainScreen"
            android:text="@string/cancelar_button" />
    </LinearLayout>
</LinearLayout>