Search code examples
androidheighttablelayouttablerowfill-parent

Table layout with rows filling parent but having same height


Does anyone know of a way to have a table layout with a number of rows having the same height but filling the parent?

In the following layout all the rows will have the same height but if I add, say an image view to the first row, this row will stretch to fit the height of the image. Is there any way to have the row only showing the part of the image view or whatever you might throw in it which can fit in the row?

<TableLayout android:id="@+id/table_layout"
                     android:layout_width="fill_parent"
                     android:layout_height="fill_parent">

    <TableRow android:id="@+id/row1"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="1"
              android:background="#FF0000FF">
    </TableRow>

    <TableRow android:id="@+id/row2"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="1"
              android:background="#FF00000">
    </TableRow>

    <TableRow android:id="@+id/row3"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="1"
              android:background="#FF00FF00">
    </TableRow>

    <TableRow android:id="@+id/row4"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="1"
              android:background="#FFFF0000">
    </TableRow>

    <TableRow android:id="@+id/row5"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="1"
              android:background="#FFFFFFFF">
    </TableRow>

</TableLayout>

Solution

  • Upon reading this, I think the best way to do it is by resizing the image first before adding it to the row.

    The children of a TableLayout cannot specify the layout_width attribute. Width is always MATCH_PARENT. However, the layout_height attribute can be defined by a child; default value is WRAP_CONTENT. If the child is a TableRow, then the height is always WRAP_CONTENT.