ikromm
ikromm

Reputation: 523

Android TableLayout stretches cell contents

Here is the issue.

I'm creating a TableLayout containing a some ImageButtons.

Each row has 3 ImageButtons but their width does not equal the width of the table. Also the same applies for their height.

All I want to do is center the ImageButtons within the cell they are in.

The

auto:StretchColumns="*"

stretches the ImageButtons (cell data), even I have specified their size in the XML file.

Also, are there any options to do the same for the rows?

Something like calculating the margins of the cells automatically.

Upvotes: 2

Views: 3748

Answers (2)

Phobos
Phobos

Reputation: 9557

Children of the Table Layout do not have to be only TableRow. The Table Layout supports children of all types including LinearLayout, TextView, ImageButton, etc.

Do not wrap your image button in a TableRow. Insert it as a row.

<TableRow>
    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Example Table Row" />
</TableRow>

<ImageButton
    android:id="@+id/ib1"
    android:layout_width="fill_parent" <---! The Parent Is The Table Layout !--->
    android:layout_height="wrap_content"
    android:background="@drawable/coolBGImage" />

<TableRow>
BLAH BLAH BLAH
</TableRow>

To put 3 ImageButtons in one row use a LinearLayout with android:orientation="horizontal"

</TableRow>

<LinearLayout
    android:id="@+id/LL1"
    android:layout_width="fill_parent"  <---! The Parent is the TableLayout !--->
    android:layout_height="wrap_content"
    android:orientation="horizontal">

        <ImageButton
            android:id="@+id/IB1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/coolBGImage1"
            android:weight="1" />

Add the next two image buttons with the same weight to make the buttons divide the row evenly.

</LinearLayout>

Upvotes: 1

kaspermoerch
kaspermoerch

Reputation: 16570

Have you tried setting the android:layout_gravity="center" for the ImageButtons?

Alternatively, you could wrap each ImageButton in a LinearLayout like this:

<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent">
   <ImageButton android:layout_gravity="center" ...other attributes... />
</LinearLayout>

Upvotes: 2

Related Questions