Question

I'm currently trying to position some LinearLayouts (which are then filled with some images) on an absolute position on the screen. Currently the LinearLayouts are alinged to the top-left of the parent RelativeLayout and then positioned using margins. This works properly for all child LinearLayouts EXCEPT the first one in the list. Setting some margins (as for example 25dp for the left margin) does not show any effect in this case. I hope someone has had the same issue and can provide some guidance.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_page"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout android:id="@+id/ll_first" android:layout_height="wrap_content" android:layout_width="wrap_content"
         android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="25dp" android:layout_marginTop="20dp"></LinearLayout>

    <LinearLayout android:id="@+id/ll_second" android:layout_height="wrap_content" android:layout_width="wrap_content"
         android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="175dp" android:layout_marginTop="30dp"></LinearLayout>

    <LinearLayout android:id="@+id/ll_third" android:layout_height="wrap_content" android:layout_width="wrap_content"
         android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="40dp" android:layout_marginTop="140dp"></LinearLayout>

</RelativeLayout>

Edit: Example image http://i.stack.imgur.com/HuYjU.jpg

Was it helpful?

Solution

The RelativeLayout provides some tags for the positioning of Views/ViewGroups relative to another View/ViewGroup.

  • android:layout_below="id" and android:layout_above="id"
    This will put the View below/above another View with the id.

  • android:layout_alignRightOf="id" and android:layout_alignLeftOf="id"
    This will put the View to the right/left of another View with the id.

All tags have the same usability like this, where I use the android:layout_below="" to set a View/ViewGroup below another:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_page"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <--! YOUR LAYOUT AT THE TOP OF YOUR PARENT -->
    <LinearLayout 
         android:id="@+id/ll_first"
         android:layout_height="wrap_content" 
         android:layout_width="wrap_content"

         android:layout_alignParentLeft="true"
         android:layout_alignParentTop="true"></LinearLayout>

    <LinearLayout android:id="@+id/ll_second"
         android:layout_height="wrap_content" 
         android:layout_width="wrap_content"

         android:layout_below="@id/ll_first" ></LinearLayout>

    <LinearLayout 
         android:id="@+id/ll_third" 
         android:layout_height="wrap_content" 
         android:layout_width="wrap_content"

         android:layout_below="@id/ll_second"></LinearLayout>

</RelativeLayout>

Now you can use some margins for the position of every View.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top