How to set Table width and number of Columns in Ta

2019-02-08 15:14发布

Is there and any method to make table like html in android where i can set row column width as we do in 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>

Edit 1:

For something like Html there the percentage width works with respect to its Parent

but the weight introduced in Android works with respect to the screen size available for root.

2条回答
Evening l夕情丶
2楼-- · 2019-02-08 15:31

You can use a LinearLayout and the weight attribute to achive this.

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1.0">

The child elements in your Rows can each be given a weight as a portion of the sum (a percentage). Be sure to set the layout_width to "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" />

Check out this previous question Linear Layout and weight in Android

查看更多
甜甜的少女心
3楼-- · 2019-02-08 15:45

The TableLayout has properties similar to LinearLayout. I did some tweek on it too. To better say it, see my sample code below. Hope its useful.

 <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>
查看更多
登录 后发表回答