Android LinearLayout Разрывы строк в XML - PullRequest
1 голос
/ 06 февраля 2012

У меня есть мероприятие, предназначенное для получения информации о клиентах. Первая строка получает имя, 2-я строка - адрес улицы, третья строка - город, штат и почтовый индекс. Я могу достичь того, что хочу, используя RelativeLayout и используя android: layout_below и android: layout_toRightOf, но тогда мне нужно специально указать ширину для моих представлений. Эта ширина может выглядеть странно, так как пользователь переориентируется или переключается между старой и новой версиями Android, где все по разному измерено. Я хочу использовать весовую характеристику LinearLayout, но все сводится к одной строке. Как вставить разрыв строки для создания строк представлений в LinearLayout?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/custLabel"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:layout_width="0dip"  
android:text="Name"/>
<EditText
android:id="@+id/customer"
android:layout_height="wrap_content"
android:layout_weight=".75"
android:layout_width="0dip"/>
<TextView
android:id="@+id/addressLabel"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:layout_width="0dip"
android:text="Address"/>
<EditText
android:id="@+id/address"
android:layout_height="wrap_content"
android:layout_weight=".75"
android:layout_width="0dip"/>
<TextView
android:id="@+id/cityLabel"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:layout_width="0dip"
android:text="City"/>
<EditText
android:id="@+id/city"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:layout_width="0dip"/>
<TextView
android:id="@+id/stateLabel"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:layout_width="0dip"
android:text="State"/>
<EditText
android:id="@+id/state"
android:layout_height="wrap_content"
android:layout_weight=".20"
android:layout_width="0dip"/>
<TextView
android:id="@+id/zipLabel"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:layout_width="0dip"
android:text="Zip"/>
<EditText
android:id="@+id/zip"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:layout_width="0dip"/>
</LinearLayout>

Ответы [ 3 ]

4 голосов
/ 06 февраля 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="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/custLabel"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text="Name" />

        <EditText
            android:id="@+id/customer"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".75" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/addressLabel"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text="Address" />

        <EditText
            android:id="@+id/address"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".75" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/cityLabel"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".1"
            android:text="City" />

        <EditText
            android:id="@+id/city"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".25" />

        <TextView
            android:id="@+id/stateLabel"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".1"
            android:text="State" />

        <EditText
            android:id="@+id/state"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".20" />

        <TextView
            android:id="@+id/zipLabel"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".1"
            android:text="Zip" />

        <EditText
            android:id="@+id/zip"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".20" />
    </LinearLayout>
</LinearLayout>

должно работать.

3 голосов
/ 06 февраля 2012

Свойство линейного вида состоит в том, что он будет располагать дочерние виды по горизонтали или вертикали.Вы не можете упорядочить каждый TextView и Edit текст в чередующихся строках.Для этого вам нужно использовать дополнительные макеты, такие как макет таблицы или даже другой линейный макет внутри текущего линейного макета.

Ниже приведен пример:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight=".25" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/custLabel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text="Name" />

        <EditText
            android:id="@+id/customer"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".75" >

            <requestFocus />

        </EditText>
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/addressLabel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text="Address" />

        <EditText
            android:id="@+id/address"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".75" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/cityLabel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".1"
            android:text="City" />

        <EditText
            android:id="@+id/city"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25" />
    </TableRow>

    <TableRow
        android:id="@+id/TableRow4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/stateLabel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".1"
            android:text="State" />

        <EditText
            android:id="@+id/state"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".20" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/zipLabel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".1"
            android:text="Zip" />

        <EditText
            android:id="@+id/zip"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25" />
    </TableRow>

</TableLayout>

1 голос
/ 06 февраля 2012

То, что вы хотите сделать, невозможно с одним LinearLayout.Вы должны проверить TableLayout .

...