Как создать макет, содержащий текстовое представление с кнопками с обеих сторон - PullRequest
2 голосов
/ 08 марта 2012

Как создать макет с кнопкой по обе стороны текстового обзора? Это то, что у меня так далеко:

<?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:orientation="horizontal" >

    <Button
        android:id="@+id/left_button"
        android:layout_width="72dip"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="left" />

    <TextView
        android:id="@+id/center_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dip"
        android:padding="10dip"
        android:text="textview" />

    <Button
        android:id="@+id/right_button"
        android:layout_width="72dip"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="right" />
</RelativeLayout>

Проблема здесь в том, что текстовое представление центрировано. Я хочу, чтобы текстовое представление полностью заполнило оставленное пространство. Я попытался установить его android: layout_width для fill_parent и удалить android: layout_centerHor Horizontal, но затем кажется, что кнопки перекрываются.

Ответы [ 5 ]

5 голосов
/ 08 марта 2012

Шаг 1

Вам нужно создать первую кнопку слева от упоминания экрана android:layout_alignParentLeft="true"

Шаг 2

создать вторую кнопку в правой части экрана с упоминанием android:layout_alignParentRight="true"

Шаг 3

Теперь создайте TextView и укажите его справа от первой кнопки и слева от второй кнопки, упомянув

android:layout_toRightOf="@id/left_button"
    android:layout_toLeftOf="@+id/right_button"

XML-код

<?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:orientation="horizontal" >

        <Button
            android:id="@+id/left_button"
            android:layout_width="72dip"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="left" />

        <Button
            android:id="@+id/right_button"
            android:layout_width="72dip"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="right" />
        <TextView
            android:id="@+id/center_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:layout_toRightOf="@id/left_button"
            android:layout_toLeftOf="@+id/right_button"
            android:padding="10dip"
            android:text="textview" />


    </RelativeLayout>
2 голосов
/ 08 марта 2012

Звучит так, как будто бы вам лучше использовать LinearLayout, что-то вроде этого:

<?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="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/left_button"
        android:layout_width="72dip"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="left" />

    <TextView
        android:id="@+id/center_textview"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:layout_weight="1"
        android:padding="10dip"
        android:text="textview" />

    <Button
        android:id="@+id/right_button"
        android:layout_width="72dip"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="right" />
</RelativeLayout>

Таким образом, все три представления будут находиться в одной горизонтальной линии, а TextView займет все оставшееся пространство.после размещения кнопок (см. свойство android: layout_weight).

0 голосов
/ 08 марта 2012

Попробуйте использовать LinearLayout с ориентацией = "горизонтальный", в отличие от RelativeLayout.

Или, если вы хотите сделать RelativeLayout, вы можете использовать атрибуты, упомянутые Чандрой (т.е. выровнять текстовое представление относительно кнопок, а не относительно родительского контейнера).

0 голосов
/ 08 марта 2012

Не уверен, что вы хотите, но вы можете использовать, например.layout_toRightOf.Пример, который выравнивает кнопку по левому краю родителя, а затем размещает вид справа от левого.

<?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:orientation="horizontal" >

<Button
    android:id="@+id/left_button"
    android:layout_width="72dip"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:text="left" />

<TextView
    android:id="@+id/center_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dip"
    android:layout_toRightOf="@id/left_button"
    android:padding="10dip"
    android:text="textview" />

<Button
    android:id="@+id/right_button"
    android:layout_width="72dip"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/center_textview"
    android:text="right" />

</RelativeLayout>
0 голосов
/ 08 марта 2012

Эти атрибуты могут решить вашу проблему

android:layout_toLeftOf
android:layout_toRightOf
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...