简体   繁体   中英

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)?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM