关于android:同一行中的两个编辑文本,不重叠

Two edit text in same line, without overlapping

我有2个EditText et1


在编辑文本中赋予权重。
enter image description here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10">

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_weight="1"
        android:ems="10" />

</LinearLayout>

您可以简单地将LinearLayoutandroid:orientation="horizontal"一起使用:

1
2
3
4
5
6
7
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <EditText />
    <EditText />
</LinearLayout>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="2">

    <EditText
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Edittext1" />

    <EditText
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Edittext2" />

</LinearLayout>

我发现的解决方案是使用android:layout_width:0dp

在优先级较低的编辑文本之一中使用此宽度。当具有较高优先级的文本变大时,第二个而不是重叠出现在屏幕之外,

这不是我所需要的100%,而是为我工作。

谢谢


将此行添加到et2的xml中:

1
android:toRightOf="@+id/et1"

这确保et2始终位于et1的右侧,因此它们不会重叠。