Подтвердить что ты не робот

Android: создание двух столбцов в линейном режиме

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Street" 
            android:layout_gravity="left"/>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="456546546"
            android:layout_gravity="right" />

    </LinearLayout>

</LinearLayout>

Я пытаюсь создать макет с двумя столбцами, с одним текстовым полем слева, а другой с правой стороны. Тем не менее, текстовые комментарии все еще находятся на левой стороне.

4b9b3361

Ответ 1

Вы должны использовать атрибут android:layout_weight. Вот пример:

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Street" 
        android:layout_gravity="left"
        android:background="#88FF0000"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="456546546"
        android:layout_gravity="right" 
        android:background="#8800FF00"/>

</LinearLayout>

enter image description here

Ответ 2

Да, это сбивает с толку. Несмотря на то, что ширина LinearLayout установлена ​​на fill_parent, она по-прежнему требует минимальной ширины. Вам нужно установить второй TextView для fill_parent, а затем его гравитацию вправо:

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Street"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="456546546"
        android:gravity="right" />

</LinearLayout>

Ответ 3

Если u хочет иметь несколько строк в каждом столбце, вы можете использовать Таблица Layout

Ответ 4

Я надеюсь, что это поможет вам.

Попробуйте этот код.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">


    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="#00FF00"
        android:paddingRight="90dp"
        android:orientation="vertical" >

        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/b1"
            android:text="Button 1"/>

    </LinearLayout>

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FF00FF"
        android:orientation="vertical" >     

        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/b2"
            android:text="Button 2"/>

    </LinearLayout>

</LinearLayout>

Ответ 5

И если вам нужен промежуток между кнопками:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.95"
        android:text="Via SMS" />

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.05" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.95"
        android:text="Diaporama" />
</LinearLayout>

Ответ 6

Попробуйте использовать таблицу. В графическом макете Перетащите таблицу, поместите элементы в ячейку.

Ответ 7

Потенциальная проблема с просто двумя вертикальными LinearLayouts для ваших столбцов заключается в том, что ничто не гарантирует, что строки будут выстраиваться в линию, если высота строк будет переменной. TableLayout лучше всего подходит для этого, а также дает вам контроль над тем, как столбцы сокращаются или расширяются, чтобы заполнить доступное пространство.

Ссылка изменилась с @santhosh-shettigar. Руководство: https://developer.android.com/guide/topics/ui/layout/grid.html
Ссылка: http://developer.android.com/reference/android/widget/TableLayout.html