maysara
maysara

Reputation: 6419

ImageView in LinearLayout

I have two problems with this layout :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="130dp"
    android:orientation="horizontal"
    android:background="@null">
        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="@drawable/image1"
            android:id="@+id/image1"
            android:scaleType="centerCrop"
            android:background="@drawable/card_background"
            android:layout_weight=".5"
            />
        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="@drawable/image2"
            android:id="@+id/image2"
            android:scaleType="centerCrop"
            android:background="@drawable/card_background"
            android:layout_weight=".5"
            />
</LinearLayout>

when I put different Images in the ImageViews , this happened :

Upvotes: 2

Views: 5270

Answers (2)

Akp
Akp

Reputation: 269

You forget to add weightsum in root layout so add this line in root layout.

android:weightSum="1"

Its like like after adding weight sum

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="130dp"
    android:weightSum="1"
    android:orientation="horizontal"
    android:background="@null">
        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="@drawable/image1"
            android:id="@+id/image1"
            android:scaleType="centerCrop"
            android:background="@drawable/card_background"
            android:layout_weight=".5"
            />
        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="@drawable/image2"
            android:id="@+id/image2"
            android:scaleType="centerCrop"
            android:background="@drawable/card_background"
            android:layout_weight=".5"
            />
</LinearLayout>

Upvotes: 2

maysara
maysara

Reputation: 6419

this code works , why ? I don't really know .

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="130dp"
    android:background="@null"
    >
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".5"
            >
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="130dp"
                android:id="@+id/image2"
                android:scaleType="centerCrop"
                android:src="@drawable/offer_mix_small_1"
                />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".5"
            android:background="@color/secondary_text_color"
            >
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="130dp"
                android:id="@+id/image1"
                android:scaleType="centerCrop"
                android:src="@drawable/offer_land"
                />
        </RelativeLayout>
    </LinearLayout>

Upvotes: 0

Related Questions