Question

First time asking a question, so please be gentle. :)

I'm trying to create a nested layout of ImageViews inside a LinearLayout. The parent LinearLayout needs to match the root view's width but have it's height determined by the 2 ImageView components it holds.

The 2 ImageView components need to be equally weighted for width (i.e. 50%) within the LinearLayout parent and have retain their proportional height.

However, whilst the width of the ImageViews are calculating correctly and the height of the image itself is correct, the height of the parent LinearLayout is not.

Below is the XML code I am using, apologies but I do not currently have enough reputation points to post images, which could make this difficult.

XML Code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/grey"
    android:orientation="vertical"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:background="@drawable/container_dropshadow"
        android:orientation="horizontal"
        android:padding="5dp"
        android:weightSum="2" >

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:padding="5dp"
            android:src="@drawable/barcode" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:padding="5dp"
            android:src="@drawable/barcode" />
    </LinearLayout>

</LinearLayout>

This is what I am getting

enter image description here

Any help would be greatly appreciated, apologies for any failing in etiquette,

Danny

Was it helpful?

Solution

You should add the following line for each ImageView:

android:adjustViewBounds="true"

For more info about the xml attribute see here

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