Макет таблицы с одинаковой высотой строк

Я создал TableLayout, где я хочу, чтобы каждая строка имела одинаковую высоту, независимо от ее содержимого. Как я могу это сделать?

Вот мой код. Каждый элемент строки получает ту же ширину, но высоты не равны. Что я делаю не так?

 <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"                    android:background="@color/grey"
                android:padding="1dip" >

                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@color/grey" >

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:layout_marginTop="2dip"
                            android:layout_weight="1"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_make"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carMake"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:layout_gravity="center_vertical"
                            android:layout_marginLeft="1dip"
                            android:layout_weight="1"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_detail" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:layout_marginTop="2dip"
                            android:layout_weight="1"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_model"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carModel"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:layout_gravity="center_vertical"
                            android:layout_weight="1"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_detail" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:layout_marginTop="2dip"
                            android:layout_weight="1"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_version"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carYear"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:layout_gravity="center_vertical"
                            android:layout_weight="1"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_year" />
                    </LinearLayout>
                </TableRow>

                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_marginTop="1dip"
                    android:layout_weight="1"
                    android:background="@color/grey" >

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:layout_marginTop="2dip"
                            android:background="@color/white"
                                android:gravity="center_horizontal"
                            android:text="@string/car_year"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carYear"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_year" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:layout_marginTop="2dip"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_mileage"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carYear"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_year" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:layout_marginTop="2dip"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_engine_capacity"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carYear"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:layout_gravity="center_vertical"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_year" />
                    </LinearLayout>
                </TableRow>

                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_marginTop="1dip"
                    android:layout_weight="1"
                    android:background="@color/grey" >

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_engine_type"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carYear"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_year" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_exterior_color"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carYear"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_year" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_city"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carYear"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_year" />
                    </LinearLayout>
                </TableRow>

                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_marginTop="1dip"
                    android:layout_weight="1"
                    android:background="@color/grey" >

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_transmittion"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carYear"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_year" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_area"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carYear"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_year" />
                    </LinearLayout>

                        <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_registered_city"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carYear"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_year" />
                    </LinearLayout>
                </TableRow>
            </TableLayout>

Ответы

Ответ 1

Ниже ответ устарел сейчас:

Вы можете изменить в своей TableRow -> LinearLayout

<LinearLayout
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"    
                    android:layout_weight="1"
                    android:background="@color/white"
                    android:orientation="vertical" >

Ответ 2

вот ваше решение:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="4" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#B0B0B0" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:orientation="vertical"
            android:weightSum="4" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#B0B0B0" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:orientation="vertical"
            android:weightSum="4" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#B0B0B0" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:orientation="vertical"
            android:weightSum="4" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#B0B0B0" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:orientation="vertical"
            android:weightSum="4" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#B0B0B0" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:orientation="vertical"
            android:weightSum="4" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#B0B0B0" />
        </TableRow>
    </TableLayout>

</LinearLayout>

enter image description here

Ответ 3

Представленный в API 11, вы можете установить android:measureWithLargestChild="true" на TableLayout и android:layout_weight="1" на каждом TableRow.

Ответ 4

вот решение:

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

       <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_gravity="fill"
        />
       <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_gravity="fill"
        />
    </TableRow>
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

       <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_gravity="fill"
        />
       <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_gravity="fill"
        />
    </TableRow>

Ответ 5

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

Хотя, как упоминалось в комментарии, я мог бы выяснить, работает ли это, просто прочитав его. Я новичок, поэтому не могу ни оставить комментарий, ни проголосовать за его использование. Поэтому я размещаю его здесь.

Идея состоит в том, что вы позволяете TableRow определять высоту строки. Установив layout_height в wrap_content, высота строки зависит от содержимого TextView.

Поскольку граничная линия ячейки будет границей TextView, layout_height должна быть match_parent.

Я хочу, чтобы это было полезно для Android newbee (да, я тоже новичок в android).

Ответ 6

<TableRow
                android:layout_width="fill_parent"
                android:layout_height="xxdip"  // HERE define a fixed size