Search code examples
androidandroid-layoutimageviewandroid-linearlayout

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 :

  • The ImageViews height is greater than 130dp however I put the height as (match_parent) and the parent height is 130dp .
  • The ImageViews width is not the same , however the both of ImageViews has the same weight , but the larger one takes more width than the other .

Solution

  • 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>