Mickmouette Steack
Mickmouette Steack

Reputation: 33

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.

Upvotes: 1

Views: 1930

Answers (4)

Amit Vaghela
Amit Vaghela

Reputation: 22945

Take LinearLayout as parent than Use two RelativeLayout in Child,

Use android:weightSum in LinearLayout with android:orientation.

Now, check this xml, you will get your required output

<?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="match_parent"
    android:orientation="horizontal"
    android:weightSum="2">

    <RelativeLayout
        android:id="@+id/rel3"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.4"
        android:gravity="center">

        <RelativeLayout
            android:id="@+id/rel21"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="55dp"
            android:background="#7F0000FF" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rel1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.6"
        android:background="#7F000000"
        android:gravity="center">

        <View
            android:id="@+id/view"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="5dp"
            android:background="#00FF00" />

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

            <View
                android:id="@+id/view2"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_centerHorizontal="true"
                android:background="#00FF00" />
        </RelativeLayout>
    </RelativeLayout>

</LinearLayout>

enter image description here

Upvotes: 0

Narendra Motwani
Narendra Motwani

Reputation: 1115

<?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:layout_alignParentEnd="true"
    android:background="#7F000000"
    android:gravity="center">

    <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_height="50dp"
        android:layout_below="@+id/view"
        android:background="#00FF00" />


</RelativeLayout>

<RelativeLayout
    android:id="@+id/rel3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true">

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

    </RelativeLayout>
</RelativeLayout>

Upvotes: 0

Iulian Popescu
Iulian Popescu

Reputation: 2643

I'm pretty sure that you can't do that from the xml file, but I am sure that you can set at runtime the LayoutParams of the rel2 to match the LayoutParams of view2, and set the root layout to FrameLayout

Upvotes: 0

Sathish Kumar J
Sathish Kumar J

Reputation: 4335

You Can't.

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

Upvotes: 1

Related Questions