在一行中对齐和调整textview和edittext的宽度

align and adjust width of textview and edittext in a row

我有一个 TextView 和 EditText,它们需要以适当的跨度对齐成一行。

在当前布局中,EditText 对齐,但看起来缩小了,没有占用剩余宽度

Xml:

 <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:style="@style/AppTheme"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp">

        <TableRow>
            <TextView
                style="@style/AppTheme.DetailInfo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Password:" />
            <EditText
                style="@style/AppTheme.TextBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:maxLines="1"
                android:text= "@={profile.password}"/>
        </TableRow>
   </TableLayout>

如果你想让EditText填充剩余的space,你应该设置它的layout_width = "0dp" 和 layout_weight = "1"。参见:

https://developer.android.com/guide/topics/ui/layout/linear.html#Weight

"For example, if there are three text fields and two of them declare a weight of 1, while the other is given no weight, the third text field without weight will not grow and will only occupy the area required by its content."

编辑:如果您希望编辑文本的开头对齐,您应该对所有文本视图应用相同的权重,并对所有编辑文本应用相同的权重。原始答案将简单地填充剩余的 space,而不保持编辑文本的开头对齐。