tags:

views:

250

answers:

2

What is the best documentation for the RelativeLayout layout algorithm?

In the layout below, the edittext gets rendered on top of the textview. This seems wrong since the EditText has android:layout_below="@id/textview".

If I remove the android:layout_centerInParent="true" on the TextView, then the EditText is pushed below the TextView. Can anyone explain the algorithm that causes this to happen? Is this the intended behavior?

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/ 
android"        android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                android:id="@+id/main" 
        > 
    <RelativeLayout android:layout_width="wrap_content" 
                    android:layout_height="wrap_content" 
                    android:layout_centerInParent="true" 
                    android:id="@+id/inner" 
            > 
        <TextView 
                android:id="@+id/textview" 
                android:layout_centerInParent="true" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:text="@string/hello" 
                /> 
        <EditText 
                android:layout_below="@id/textview" 
                android:id="@+id/textfield" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:text="@string/hello" 
                /> 
    </RelativeLayout> 
</RelativeLayout> 
+1  A: 

Not 100% sure about this, but what about the "+"-sign for the ID?

Also, why are you nesting RelativeLayouts? Thats what they're made for avoiding :)

sandos
The + sign is used when you are creating a new id. When you are referencing an id, you don't need the + sign.
Mayra
I am not sure why, but I am having to always use the + in my XMLs.. it gives me compile error if I try to drop it from the points where I am just referencing for relative layout position
+1  A: 

From the relativeLayout docs:

Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM.

I think you may be running into this issue, try changing the layout_width and layout_height of your relative layout to fill_parent.

Mayra