Как установить ширину таблицы и количество столбцов в TableLayout на Android
Есть ли какой-либо метод для создания таблицы как html в android, где я могу установить ширину столбца строки, как в html
<table >
<tr><td style="width:'50%;"></td>
<td style="width:'50%;"></td>
</tr>
</table>
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="3" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
Изменить 1:
Для чего-то вроде Html, процентная ширина работает в отношении родительского
но вес, введенный в Android, работает в отношении размера экрана, доступного для
корень.
Ответы
Ответ 1
Вы можете использовать LinearLayout и атрибут веса для достижения этого.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1.0">
Детским элементам в ваших рядах каждому присваивается вес как часть суммы (в процентах). Не забудьте установить layout_width в "0dp"
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.5" />
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.5" />
Проверьте этот предыдущий вопрос
Линейная компоновка и вес в Android
Ответ 2
В TableLayout есть свойства, похожие на LinearLayout. Я тоже немного подружился. Чтобы лучше сказать это, см. Мой пример кода ниже. Надеюсь, что это полезно.
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="3" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<TextView android:text="@string/test1" android:layout_weight="1"></TextView>
<TextView android:text="@string/test2" android:layout_weight="1"></TextView>
<TextView android:text="@string/test3" android:layout_weight="1"></TextView>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<TextView android:text="@string/test1" android:layout_weight="1"></TextView>
<TextView android:text="@string/test2" android:layout_weight="1"></TextView>
<TextView android:text="@string/test3" android:layout_weight="1"></TextView>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<TextView android:text="@string/test1" android:layout_weight="1"></TextView>
<TextView android:text="@string/test2" android:layout_weight="1"></TextView>
<TextView android:text="@string/test3" android:layout_weight="1"></TextView>
</TableRow>
</TableLayout>