Search code examples
androidandroid-layoutandroid-relativelayout

How to let an image matches another image's height using RelativeLayout?


I wanted to put ImageB on top of ImageA. How should I achieve that?

enter image description here

Note:

  • I don't want to fix my height for both images so that I can maintain their aspect ratio.
  • The height for ImageA and ImageB are different.

    <RelativeLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
    
        <ImageView
        android:id="@+id/imageA"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:scaleType="fitCenter"
        android:src="@drawable/imageA" 
        android:adjustViewBounds="true"/>
    
        <!-- How to let imageB matches imageA's height ? -->
        <ImageView
        android:id="@+id/imageB"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/imageB"/>
    </RelativeLayout>
    

Any help will be appreciated.


Solution

  • Try this code

    <RelativeLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
    
        <ImageView
        android:id="@+id/imageA"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:scaleType="fitCenter"
        android:src="@drawable/imageA" 
        android:adjustViewBounds="true"/>
    
        <!-- How to match let imageB matches imageA's height ? -->
        <ImageView
        android:id="@+id/imageB"
            android:layout_alignTop="@id/imageA" // add it
            android:layout_alignBottom="@id/imageA" // add it
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:layout_alignParentTop="true" // remove it
            android:layout_alignParentRight="true"
            android:src="@drawable/imageB"/>
    </RelativeLayout>
    

    Hope this help