I would like to ask you, how can I put two EditText
on the sa开发者_如何学JAVAme line?
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.
There are multiple ways, here are 2.
With a horizontal LinearLayout
Assign android:orientation="horizontal"
to your outer LinearLayout
. This way all child elements of this layout will be aligned next to each other.
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.
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
)
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>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/username_id"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
app:shapeAppearance="@style/Rounded">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="User Name"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
app:shapeAppearance="@style/Rounded"
android:layout_toRightOf="@+id/username_id">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/password_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
</RelativeLayout>
精彩评论