I would like to ask you, how can I put two EditText
on the same line?
我想问你,我怎么能把两个EditText放在同一条线上?
3 个解决方案
#1
17
Give this a shot:
给这一枪:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
Also, you should try accepting answers if you get one that answers your question. You'll be more likely to get more/better responses.
此外,如果你得到一个能回答你问题的答案,你应该试着接受它。你会得到更多/更好的回答。
#2
5
There are multiple ways, here are 2.
有多种方法,这里是2。
With a horizontal LinearLayout
与LinearLayout水平
Assign android:orientation="horizontal"
to your outer LinearLayout
. This way all child elements of this layout will be aligned next to each other.
将android:orientation=“水平”分配到您的外部线性布局。这样,该布局的所有子元素都将彼此对齐。
Semi-layout:
Semi-layout:
<LinearLayout android:orientation="horizontal">
<EditText />
<EditText />
</LinearLayout>
With a RelativeLayout
与使用
Use android:layout_toLeftOf="@id/otheredittext"
or android:layout_toRightOf="@id/.."
to tell one of the EditTexts that it belongs to the right/left of the other one and align the first one relative to the parent (the RelativeLayout
) by using android:layout_alignParentTop="true"
, same with left, right or bottom.
用android:layout_toLeftOf=“@id/otheredittext”或android:layout_toRightOf="@id/..",告诉其中一个EditTexts,它属于另一个的右/左,并通过使用android:layout_alignParentTop="true",将第一个edittext与父(RelativeLayout)相匹配,与left、right或bottom相同。
Semi-layout:
Semi-layout:
<RelativeLayout>
<EditText android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:id="@+id/edittext1"
/>
<EditText android:layout_toRightOf="@id/edittext1" />
</RelativeLayout>
(Also notice that you have a +id
when assigning the id for the first time in android:id
and when you reference it from the layout via android:layout_to...
, it's just id
)
(还要注意,在android:id中第一次分配id时,您有一个+id,当您通过android:layout_to…,它只是id)
#3
1
For Linear layout it will be as follows:
线性布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<EditText android:id="@+id/editText1" android:layout_height="wrap_content" android:layout_width="100dip">
<requestFocus></requestFocus>
</EditText>
<EditText android:layout_height="wrap_content" android:id="@+id/editText2" android:layout_width="100dip"></EditText>
</LinearLayout>
For relative layout it will be as follows:
相对布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:id="@+id/editText1" android:layout_height="wrap_content" android:layout_width="100dip">
<requestFocus></requestFocus>
</EditText>
<EditText android:layout_height="wrap_content" android:layout_width="100dip" android:id="@+id/editText2" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"></EditText>
</RelativeLayout>
#1
17
Give this a shot:
给这一枪:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
Also, you should try accepting answers if you get one that answers your question. You'll be more likely to get more/better responses.
此外,如果你得到一个能回答你问题的答案,你应该试着接受它。你会得到更多/更好的回答。
#2
5
There are multiple ways, here are 2.
有多种方法,这里是2。
With a horizontal LinearLayout
与LinearLayout水平
Assign android:orientation="horizontal"
to your outer LinearLayout
. This way all child elements of this layout will be aligned next to each other.
将android:orientation=“水平”分配到您的外部线性布局。这样,该布局的所有子元素都将彼此对齐。
Semi-layout:
Semi-layout:
<LinearLayout android:orientation="horizontal">
<EditText />
<EditText />
</LinearLayout>
With a RelativeLayout
与使用
Use android:layout_toLeftOf="@id/otheredittext"
or android:layout_toRightOf="@id/.."
to tell one of the EditTexts that it belongs to the right/left of the other one and align the first one relative to the parent (the RelativeLayout
) by using android:layout_alignParentTop="true"
, same with left, right or bottom.
用android:layout_toLeftOf=“@id/otheredittext”或android:layout_toRightOf="@id/..",告诉其中一个EditTexts,它属于另一个的右/左,并通过使用android:layout_alignParentTop="true",将第一个edittext与父(RelativeLayout)相匹配,与left、right或bottom相同。
Semi-layout:
Semi-layout:
<RelativeLayout>
<EditText android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:id="@+id/edittext1"
/>
<EditText android:layout_toRightOf="@id/edittext1" />
</RelativeLayout>
(Also notice that you have a +id
when assigning the id for the first time in android:id
and when you reference it from the layout via android:layout_to...
, it's just id
)
(还要注意,在android:id中第一次分配id时,您有一个+id,当您通过android:layout_to…,它只是id)
#3
1
For Linear layout it will be as follows:
线性布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<EditText android:id="@+id/editText1" android:layout_height="wrap_content" android:layout_width="100dip">
<requestFocus></requestFocus>
</EditText>
<EditText android:layout_height="wrap_content" android:id="@+id/editText2" android:layout_width="100dip"></EditText>
</LinearLayout>
For relative layout it will be as follows:
相对布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:id="@+id/editText1" android:layout_height="wrap_content" android:layout_width="100dip">
<requestFocus></requestFocus>
</EditText>
<EditText android:layout_height="wrap_content" android:layout_width="100dip" android:id="@+id/editText2" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"></EditText>
</RelativeLayout>