Search code examples
androidandroid-layoutandroid-relativelayout

Align a layout with a child of another layout


Is there a way to align a layout with a child of another layout? I have created an example :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout 
        android:id="@+id/rel1"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:background="#7F000000"
        android:gravity="center"
        android:layout_alignParentEnd="true">
        <View 
            android:id="@+id/view"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginBottom="5dp"
            android:background="#00FF00"/>
        <View 
            android:id="@+id/view2"
            android:layout_width="50dp"
            android:layout_below="@+id/view"
            android:layout_height="50dp"
            android:background="#00FF00"/>
    </RelativeLayout>

    <RelativeLayout 
        android:id="@+id/rel2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#7F0000FF"
        android:layout_alignTop="@+id/view2">

    </RelativeLayout>
</RelativeLayout>

Here I try to align the top of the RelativeLayout rel2 with the top of view2, but RelativeLayout don't enable to do that.

I have :

enter image description here

I want :

enter image description here

Thanks.


Solution

  • You Can't.

    Layout constraints in a given RelativeLayout should reference other views within the same relative layout (but not itself!)