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

Выравнивание макета стола Android

Я использую Table Layout для отображения данных, как показано ниже. enter image description here

Что я хочу сделать?

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

Код:

    <TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tableLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp" >

            <ImageView
                android:id="@+id/place_category_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:contentDescription="ss"
                android:paddingRight="10dp"
                android:src="@drawable/icon_category"
                android:textAlignment="textStart" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_gravity="center"
                android:text="230 kms"
                android:textSize="16sp" >
            </TextView>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp" >

            <ImageView
                android:id="@+id/place_category_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:contentDescription="ss"
                android:paddingRight="10dp"
                android:src="@drawable/icon_category"
                android:textAlignment="textStart" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_gravity="center"
                android:text="Hill Station, Wild Life"
                android:textSize="16sp" >
            </TextView>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp" >

            <ImageView
                android:id="@+id/place_category_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:contentDescription="ss"
                android:paddingRight="10dp"
                android:src="@drawable/icon_category"
                android:textAlignment="textStart" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_gravity="center"
                android:text="Summer 23-40°C, Winter 10-32°C"
                android:textSize="16sp" >
            </TextView>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp" >

            <ImageView
                android:id="@+id/place_category_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:contentDescription="ss"
                android:paddingRight="10dp"
                android:src="@drawable/icon_category"
                android:textAlignment="textStart" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_gravity="center"
                android:text="Tippus Drop, Tippus Summer Residence, Brahmashram, Cycling, Paragliding"
                android:textSize="16sp" >
            </TextView>
        </TableRow>
    </TableLayout>

Как это должно выглядеть

enter image description here

4b9b3361

Ответ 1

Самый простой способ - обернуть каждый TableRow контент с помощью LinearLayout следующим образом:

<TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tableLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp" >

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

                <ImageView
                    android:id="@+id/place_category_icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:contentDescription="ss"
                    android:paddingRight="10dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center"
                    android:text="230 kms"
                    android:textSize="16sp" >
                </TextView>
            </LinearLayout>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp" >

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

                <ImageView
                    android:id="@+id/place_category_icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:contentDescription="ss"
                    android:paddingRight="10dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center"
                    android:text="Hill Station, Wild Life"
                    android:textSize="16sp" >
                </TextView>
            </LinearLayout>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp" >

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

                <ImageView
                    android:id="@+id/place_category_icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:contentDescription="ss"
                    android:paddingRight="10dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center"
                    android:text="Summer 23-40°C, Winter 10-32°C"
                    android:textSize="16sp" >
                </TextView>
            </LinearLayout>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp" >

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

                <ImageView
                    android:id="@+id/place_category_icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:contentDescription="ss"
                    android:paddingRight="10dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center"
                    android:text="Tippus Drop, Tippus Summer Residence, Brahmashram, Cycling, Paragliding"
                    android:textSize="16sp" >
                </TextView>
            </LinearLayout>
        </TableRow>
    </TableLayout>

И это результат:

layout

Надеюсь, я правильно понял ваши требования.

Ответ 2

Так как TableRow в значительной степени ориентирован по горизонтали LinearLayout, просто используйте принципы весов, чтобы сделать заполнение TextView все пространство после ImageView. Это означает, что вам придется изменить строки примерно так:

    <!-- no need to set width/height as those are implicitly enforced -->
    <TableRow
        android:id="@+id/tableRow2"
        android:paddingBottom="10dp" >

        <ImageView
            android:id="@+id/place_category_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="ss"
            android:paddingRight="10dp"
            android:src="@drawable/icon_category" />

        <!-- width of '0' and weight of '1' will make this view fill up all remaining space -->
        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"
            android:text="230 kms"
            android:textSize="16sp" />

    </TableRow>

Я также удалил/изменил некоторые лишние атрибуты. Имейте в виду, что я просто набрал это прямо в браузере - может потребоваться нечетная опечатка или настройка.

Ответ 3

Мое предположение - измените layout_width и избавьтесь от силы тяжести:

 <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            ...
            >
        </TextView>