Сделайте ячейки одинаковой ширины и высоты, чтобы заполнить таблицу

Я пытаюсь научиться использовать макеты таблиц в Android, это мой код:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <!-- 3 columns -->
    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dip" >

        <TextView
            android:id="@+id/textView2"
            android:text="Column 1" />

        <Button
            android:id="@+id/button2"
            android:text="Column 2" />

        <Button
            android:id="@+id/button3"
            android:text="Column 3" />
    </TableRow>
</TableLayout>

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

введите здесь описание изображения

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


person BackSlash    schedule 05.11.2013    source источник


Ответы (3)


Вам нужно поиграть со значениями weight. XML для двух строк будет выглядеть так.-

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- 3 columns -->

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:padding="5dip" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Column 1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Column 2" />

        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Column 3" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:padding="5dip" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Column 1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Column 2" />

        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Column 3" />
    </TableRow>

</TableLayout>

И вот результат.

введите здесь описание изображения


В качестве примечания обратите внимание, что fill_parent устарело, вместо этого вы должны использовать match_parent.

person ssantos    schedule 05.11.2013

Самый быстрый способ — использовать layout_weight с объектами в строке таблицы. Вот как должен выглядеть ваш xml после его указания.

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <!-- 3 columns -->
    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dip" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Column 1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Column 2" />

        <Button
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Column 3" />
    </TableRow>
</TableLayout>

При использовании веса вы хотите установить layout_width на 0 dp, чтобы он мог переопределить фактическое значение. Это разделение даст вам каждое представление, занимающее 1/3 TableRow. Поиграйте со значением веса, пока не получите то, что вам нравится.

person Andrew Schuster    schedule 05.11.2013

добавьте android:layout_weight="0.3" к дочернему внутреннему элементу TableRow попробуйте заменить свой макет xml на этот

надеюсь, это то, что вы ищете

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <!-- 3 columns -->
    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:padding="5dip" >

        <TextView
            android:id="@+id/textView2"
            android:layout_weight="0.3"
            android:text="Column 1" />

        <Button
            android:id="@+id/button2"
            android:layout_weight="0.3"
            android:text="Column 2" />

        <Button
            android:id="@+id/button3"
            android:layout_weight="0.3"
            android:text="Column 3" />
    </TableRow>
</TableLayout>
person Jack K Fouani    schedule 05.11.2013