Ответ 1
Попробуйте добавить android:stretchColumns="*"
к тегу <TableLayout>
.
У меня есть таблица с двумя строками, каждая строка имеет 3 кнопки. Как я могу заставить кнопки заполнять пространство одинаково. В HTML я бы дал им ширину 33%.
Также вы знаете, каким образом я могу создать представление, имеющее 4 кнопки изображения в строке в виде макета сетки, подобно панели запуска.
Попробуйте добавить android:stretchColumns="*"
к тегу <TableLayout>
.
Наконец-то я нашел истинный ответ: http://androidadvice.blogspot.com/2010/10/tablelayout-columns-equal-width.html
В stackoverflow также есть более минимальный пример: fooobar.com/questions/105983/...
Пример:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TableRow>
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:text="Why is doing layouts on Android"
android:layout_weight="1"
/>
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:text="so"
android:layout_weight="1"
/>
</TableRow>
<TableRow>
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:text="damn"
android:layout_weight="1"
/>
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:text="frustrating?"
android:layout_weight="1"
/>
</TableRow>
</TableLayout>
Задайте layout_width TableRow для fill_parent и задайте макет с размером 1 на каждую кнопку.
layout_weight работает как процент. Если все ваши предметы получают одинаковый номер, они занимают одинаковый процент пространства. Если одна кнопка имеет вес 2, а другая имеет вес 1, то первая занимает в два раза больше места.
Если вы еще этого не сделали, зайдите на страницу общих макетов страницы руководства для хорошего ввода в макеты.
Итак, в заключение сообщений правильный способ сделать это - установить NumberOfTheColumns
:
<tablelayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="NumberOfTheColumns"/>
Вы можете установить, сколько горизонтальных "кусков" вам нужно. Если у вас есть таблица 3x3:
<TableLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tableLayout1"
android:stretchColumns="3">
<TableRow
android:id="@+id/tableRow1">
<TextView
android:text="@string/EventTitle"
android:id="@+id/textView1"
android:gravity="center_vertical"
android:layout_column="0" />
<EditText
android:inputType="text"
android:id="@+id/editText3"
android:layout_weight="2"
android:layout_span="2"
android:layout_column="1" />
</TableRow>
<TableRow
android:id="@+id/tableRow2">
<TextView
android:text="@string/EventDateStart"
android:id="@+id/textView1"
android:gravity="center_vertical"
android:layout_column="0" />
<EditText
android:inputType="date"
android:id="@+id/editText1"
android:layout_weight="1"
android:layout_column="1"
android:layout_width="wrap_content" />
<EditText
android:inputType="time"
android:id="@+id/editText2"
android:layout_weight="1"
android:layout_column="2"
android:layout_width="match_parent" />
</TableRow>
<TableRow
android:id="@+id/tableRow3">
<TextView
android:text="@string/EventDateEnd"
android:id="@+id/textView1"
android:gravity="center_vertical"
android:layout_column="0"
android:layout_width="match_parent" />
<EditText
android:inputType="date"
android:id="@+id/editText1"
android:layout_weight="1"
android:layout_column="1" />
<EditText
android:inputType="time"
android:id="@+id/editText2"
android:layout_weight="1"
android:layout_column="2" />
</TableRow>
<TableRow>
<TextView
android:text="@string/Description"
android:id="@+id/textView1"
android:gravity="center_vertical"
android:layout_weight="3"
android:layout_span="3"
android:layout_column="0"
android:layout_width="match_parent" />
</TableRow>
<TableRow>
<EditText
android:inputType="textMultiLine"
android:id="@+id/editText3"
android:layout_height="50dp"
android:layout_weight="3"
android:layout_span="3"
android:layout_column="0"
android:layout_width="match_parent" />
</TableRow>
</TableLayout>
В этом примере вы можете увидеть таблицу 5x3. Вы можете установить каждую ширину столбца, указав, сколько частей полной ширины таблицы вам нужно...