Search code examples
androidandroid-layoutalignmentandroid-relativelayout

RelativeLayout set view to the right of a centered view


I try to set a view to the right of a first view and bottom align them inside a RelativeLayout.

The following code looks to me like it should work.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:background="#999999"
    android:padding="10dp" >

    <View
        android:id="@+id/v1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:background="#FFFFFF" />

    <View
        android:id="@+id/v2"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignBottom="@id/v1"
        android:layout_toRightOf="@id/v1"
        android:background="#FF0000" />

</RelativeLayout>

But the result is not really what i've expected.

Result

What is the problem here?

Just to be clear what i expect: both cubes should be bottom aligned against each other and the red cube should be to the right of the white one (outside).

Edit:

I have found the problem. it's not this layout, but the parent list, where it is included (it's a propriety HorizontalListView). it seems to resize its child views somehow and that causes the problem.


Solution

  • <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="300dp"
                    android:layout_height="300dp"
                    android:background="#999999"
                    android:padding="10dp" >
    
        <View
                android:id="@+id/v1"
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_centerInParent="true"
                android:background="#FFFFFF" />
    
        <View
                android:id="@+id/v2"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="#FF0000" 
                android:layout_alignBottom="@+id/v1" 
                android:layout_alignBaseline="@+id/v1"
                android:layout_alignRight="@+id/v1"/>
    
    </RelativeLayout>
    

    enter image description here

    Am I right? ;)