0

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
venni
  • 594
  • 1
  • 4
  • 19
  • I think that part of what a table layout is supposed to do is give you columns that line up. You want columns that don't line up. I'm not sure its the right thing for you. – HalR Apr 11 '13 at 02:12

2 Answers2

2

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>
Yojimbo
  • 23,288
  • 5
  • 44
  • 48
0

Yojimbo, you are right, however if you use

android:layout_weight

you must remember about replacing (in this special case) android:layout_width="wrap_content" with

android:layout_width="0dp" .

Here you have a nice reference.

Community
  • 1
  • 1
mmBs
  • 8,421
  • 6
  • 38
  • 46