Search code examples
androidhtmllayoutandroid-tablelayout

Table layout in android (span/shrink)?


I want this special table layout:

Layout of table

In my current layout the cells that are shorter are at the same size like the cells with the long text:

wrong layout

How do have to do it?

It should look like the first picture.


Solution

  • There may be better ways of doing this, I actually haven't used TableLayout before, but the XML below gives the basic layout that you asked for:

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_span="4"
                    android:layout_weight="1"
                    android:text="Text"
                    android:gravity="center" />
            </TableRow>
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.25"
                    android:text="Text" />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_span="3"
                    android:layout_weight="0.75"
                    android:text="Text that is longer than other cells" />
            </TableRow>
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.25"
                    android:text="Text" />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.25"
                    android:text="Text" />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.25"
                    android:text="Text" />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.25"
                    android:text="Text" />
            </TableRow>
        </TableLayout>