Android EditText и кнопка на одной строке
Мне нужно иметь Edittext
рядом с Search button
. Edittext
должен заполнить как можно большую ширину, кнопка должна быть вправо и быть достаточно большой, чтобы иметь текст.
Теперь он выглядит следующим образом:
[EDIT TEXT][Search]
но должен выглядеть следующим образом:
[.......EDIT TEXT.......][Search]
Вот XML
:
<RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content">
<EditText android:id="@+id/search_box"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="type to filter"
android:inputType="text"
android:maxLines="1" />
<Button android:id="@+id/search_text"
android:layout_toRightOf="@id/search_box"
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
Ответы
Ответ 1
Должен ли быть относительный макет?
Я бы предложил следующее: установить EditText
layout_width
в fill_parent
и его layout_weight
в 1, что-то вроде этого:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="fill_parent">
<EditText android:text="@+id/EditText01"
android:id="@+id/EditText01"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="fill_parent">
</EditText>
<Button android:text="@+id/Button01"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
Ответ 2
В этом случае ширина для EditText
должна быть установлена на fill_parent
(вместо wrap_content
):
<RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content">
<EditText android:id="@+id/search_box"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="type to filter"
android:inputType="text"
android:maxLines="1" />
<Button android:id="@+id/search_text"
android:layout_toRightOf="@id/search_box"
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
Изменить
Как вы сказали, он скрывает кнопку. Затем просто инвертируйте порядок:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/RelativeLayout01" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="@+id/search_text"
android:layout_alignParentRight="true"
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/search_box"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/search_text"
android:layout_centerVertical="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="type to filter"
android:inputType="text"
android:maxLines="1" />
</RelativeLayout>
Ответ 3
Я считаю, что ответ Кристиана работает над более поздними версиями SDK, но для предыдущих версий вам нужно сначала определить Button и поместить EditText в layout_toLeftOf = "@id/search_box", который Кстати, оба объекта должны иметь разные идентификаторы.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/search_button"
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
/>
<EditText
android:id="@+id/search_text"
android:layout_toLeftOf="@+id/search_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="type to filter"
android:inputType="text"
android:maxLines="1"
/>
</RelativeLayout>
Ответ 4
попробуйте это, я немного исправил. он показывает кнопку справа от редактирования текста, а также редактирует ширину текста, разрешенную для увеличения.
<?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="wrap_content"
android:padding="10dip">
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="instructions"/>
<EditText
android:id="@+id/entry"
android:layout_width="250dip"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_below="@id/label"/>
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="1dip"
android:text="ok" />
</RelativeLayout>
Ответ 5
Вот этот точный пример (выровняйте два элемента управления одной строкой):
http://developer.android.com/guide/topics/ui/layout/relative.html