Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinearLayout ImageView Resize All Image Widths To Fit

I have a horizontal linear layout that contains 3 image views. Each image view contains the same image which is 200x200 pixels. Here is my layout xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/image200" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/image200" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/image200" />

    </LinearLayout>

</RelativeLayout>

And here is what it looks like:

linear layout test screenshot

Notice how the third image from the left is resized I assume because there is not enough real estate to contain 3x 200 pixel wide images.

What I would rather happen is instead of shrinking only the last image, all three images are resized evenly so that they all fit across the screen. How do I setup my layout to evenly fit all three images?

Thank you

UPDATE - after changing my settings here is what things look like:

updates

Notice that the border around the image view is still 200 px high. Why is this happening?

like image 814
Jan Tacci Avatar asked Mar 03 '13 18:03

Jan Tacci


3 Answers

Set Width and height to match_parent for imageviews and set layout_weight=1 for all imageviews. Also set Attribute android:scaleType="fitXY"

like image 93
Pragnani Avatar answered Nov 23 '22 18:11

Pragnani


This worked perfectly for me:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:adjustViewBounds="true"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <ImageView
        android:adjustViewBounds="true"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <ImageView
        android:adjustViewBounds="true"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

</LinearLayout>

Layout width when set to 0dp, layout width is what came into picture. And here layout width of the LinearLayout is match parent. So it will take the whole width, and as per the layout width of the images, they take space in the ratio of there weight.

like image 31
blackjack Avatar answered Nov 23 '22 19:11

blackjack


make your three images a width of 0dp

and add them a layout_weight of 1

;)

<ImageView
            android:id="@+id/imageView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/image200" />
like image 35
Guian Avatar answered Nov 23 '22 18:11

Guian