dgzz
dgzz

Reputation: 3009

LinearLayout not wrapping content

I used LinearLayout to stack 4 images horizontally and it works well except for the height. It seems that the linearlayout is not properly wrapping its content. If I don't set the images' scale type to fitStart, they will be centered inside the linearlayout.

Here is my layout below:

linearlayout

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:orientation="horizontal"
    android:weightSum="4"
    android:layout_marginLeft="40dp"
    android:layout_marginRight="40dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@drawable/header1"
        android:layout_weight="1"
        android:scaleType="fitStart" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView2"
        android:layout_gravity="center_vertical"
        android:src="@drawable/header2"
        android:layout_weight="1"
        android:scaleType="fitStart" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView3"
        android:layout_gravity="center_vertical"
        android:src="@drawable/header3color"
        android:layout_weight="1"
        android:scaleType="fitStart" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView4"
        android:src="@drawable/header4"
        android:layout_weight="1"
        android:scaleType="fitStart" />
</LinearLayout>

Upvotes: 1

Views: 3029

Answers (3)

Muthuraja
Muthuraja

Reputation: 551

android:adjustViewBounds="true" to be set in your imageview's

Upvotes: -1

James McCracken
James McCracken

Reputation: 15766

Try adding android:adjustViewBounds="true" to your ImageViews

Upvotes: 2

Steven Roberts
Steven Roberts

Reputation: 300

Try setting the adjustViewBounds attribute to true for those images. http://developer.android.com/reference/android/widget/ImageView.html#attr_android:adjustViewBounds

That should take care of that extra space you have and the LinearLayout should wrap_content correctly.

Upvotes: 3

Related Questions