androidandroid-studioandroid-layoutandroid-tablelayout

Android heights of table rows using weight


I want to create a table with 4 rows, in which 3 rows are of equal height and one row is only half as high.

I thought I should be able to do so using a code like this:

<TableLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:weightSum="3.5">
        <TableRow
            android:layout_height="0dp"
            android:layout_weight="1" >
            <View
                android:background="@color/dark_grey"
                android:layout_height="match_parent"
                android:layout_width="400dp">
            </View>
        </TableRow>
        <TableRow
            android:layout_height="0dp"
            android:layout_weight="1" >
            <View
                android:background="@color/orange"
                android:layout_height="match_parent"
                android:layout_width="400dp">
            </View>
        </TableRow>
        <TableRow
            android:layout_height="0dp"
            android:layout_weight="0.5" >
            <View
                android:background="@color/purple_200"
                android:layout_height="match_parent"
                android:layout_width="400dp">
            </View>
        </TableRow>
        <TableRow
            android:layout_height="0dp"
            android:layout_weight="1" >
            <View
                android:background="@color/dark_grey"
                android:layout_height="match_parent"
                android:layout_width="400dp">
            </View>
        </TableRow>
    </TableLayout>

However, the row with weight 0.5 is way larger than the other 3.

TableLayout

Could someone please tell me what I am doing wrong? I guess I got something pretty wrong here...

Thanks celdrion


Solution

  • Just remove height field from table row and add android:layout_width="match_parent"

    Do let me know if you face any other issue. Thanks