hexagoncode
hexagoncode

Reputation: 141

How to set RelativeLayout's width same as sibling View?

So i have layout like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center">

    <RelativeLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/advantage_1" />
        <ImageView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/advantage_2" />
    </RelativeLayout>

    <ImageView
        android:id="@+id/lower_image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/line" />
</LinearLayout>

Because of aligning views inside the RelativeLayout it fills all the screen. How to set RelativeLayout's width same as lower ImageView(lower_image_view)?

Upvotes: 1

Views: 2563

Answers (1)

Md. Arafat Al Mahmud
Md. Arafat Al Mahmud

Reputation: 3214

Try like this. The main idea is you put your RelativeLayout and the ImageView inside another RelativeLayout. Then set android:layout_alignLeft="@+id/lower_image_view" and android:layout_alignRight="@+id/lower_image_view" of the child RelativeLayout. This will achieve the width of the ImageView. You may want to modify this to achieve your desired result, but you get the idea :)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center">

    <RelativeLayout
        android:id="@+id/parentRelativeLayout"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">

        <RelativeLayout
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignLeft="@+id/lower_image_view"
            android:layout_alignRight="@+id/lower_image_view"
            android:orientation="horizontal">

            <ImageView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_launcher" />

            <ImageView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_launcher" />
        </RelativeLayout>

        <ImageView
            android:id="@+id/lower_image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_launcher" />
    </RelativeLayout>
</LinearLayout>

Upvotes: 4

Related Questions