Question

I have a TableLayout with 3 TableRows, each TableRow has 3 ImageViews. Each ImageView has fitCenter for Scale type, and a weight and width per this stackoverflow answer. Now all the images scale to fit into the row, but each row is way taller then it needs to be. Here's an excerpt of the xml:

<TableLayout android:id="@+id/TableLayout01"
android:layout_height="wrap_content"
android:padding="10dip"
android:stretchColumns="0,1,2"
android:shrinkColumns="0,1,2"
android:layout_weight="1"
android:layout_width="wrap_content">
    <TableRow android:id="@+id/TableRow01"
    android:layout_width="fill_parent"
    android:layout_gravity="center_vertical|center_horizontal"
    android:baselineAligned="false"
    android:gravity="center_vertical|center_horizontal"
    android:layout_height="wrap_content">
        <ImageView android:id="@+id/image1"
        android:src="@drawable/avocado3"
        android:scaleType="fitCenter"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="wrap_content"/>
        <ImageView android:id="@+id/image2"
        android:src="@drawable/avocado3"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"/>
        <ImageView android:id="@+id/image3"
        android:src="@drawable/avocado3"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"/>
    </TableRow>
...
</TableLayout>

And here's the first row:

first row

And the end of the first row and start of the second:

second row

The ImageViews are set to a drawable that is small (@drawable/avocado3), and in the activity's onCreate() it fetches 9 images from the internet and calls setImageBitmap().

Anyone have any ideas on what I'm doing wrong?

Was it helpful?

Solution

By default, scaleType only changes the size of the actual image, but not the size of the surrounding ImageView. Since the height of the ImageView is set to wrap_content, it will take all the space it needs to display the full image, and then scale the image to fit it inside it's width.

Try setting adjustViewBounds to true. If that doesn't help, try setting a fixed layout_height or play around with the maxHeight property.

It might also help to take a look inside the hierarchy viewer: http://developer.android.com/guide/developing/tools/hierarchy-viewer.html.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top