如何在Android中设置TableLayout中的表格宽度和列数

Tri*_*shi 13 xml android-widget android-layout

是否有任何方法在android中制作像html这样的表,我可以像在html中那样设置行列宽

<table >
<tr><td style="width:'50%;"></td>
    <td style="width:'50%;"></td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

编辑1:

对于像Html这样的东西,百分比宽度相对于它的父作用

但Android中引入的权重与root可用的屏幕大小有关.

小智 22

您可以使用LinearLayout和weight属性来实现此目的.

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1.0">
Run Code Online (Sandbox Code Playgroud)

您的行中的子元素可以作为总和的一部分(百分比)给予权重.一定要将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" />
Run Code Online (Sandbox Code Playgroud)

在Android中查看此前一个问题 线性布局和重量


小智 17

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>
Run Code Online (Sandbox Code Playgroud)