Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RelativeLayout -- how to position?

I want a Layout like this: Desired layout

But my code doesn't work. I can't achieve this Layout, and I don't know what is wrong with what I've done so far.

Here's what I have so far -- Is the layout_gravity OK? Or does it need to be set it in the RelativeLayout?

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

    <RelativeLayout     
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

        <TextView
            android:id="@+id/topText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dip"
            android:layout_gravity="top" />

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/centerLayout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:layout_below="@id/topText">     

            <ImageButton
                android:id="@+id/lektionBackButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:src="@drawable/back"
                android:layout_gravity="left"/>  
            <ImageView
                android:id="@+id/centerImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:cropToPadding="true"
                android:layout_gravity="center"/>
            <ImageButton 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/lektionForwardButton"
                android:src="@drawable/forward"
                android:layout_gravity="right"/>        
         </LinearLayout>    

         <TextView
            android:id="@+id/bottomText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dip"
            android:layout_below="@id/centerLayout"
            android:layout_gravity="bottom" />
    </RelativeLayout>
</RelativeLayout>
like image 428
krackmoe Avatar asked Dec 13 '11 22:12

krackmoe


2 Answers

You don't need to use gravity or the inner LinearLayout at all for this. Instead use layout_alignParentTop, layout_alignParentLeft, layout_alignParentRight, layout_alignParentBottom, layout_centerInParent and layout_centerVertical on the children.

It might be useful for you to go through the RelativeLayout tutorial.

like image 96
kabuko Avatar answered Oct 12 '22 07:10

kabuko


Only when it comes to a Relative layout, you can switch to the Graphical layout in your Eclipse editor and drag and set the attributes. Everything with just your mouse. This however isn't possible with any other layout. Hope that helps.

like image 34
Ghost Avatar answered Oct 12 '22 07:10

Ghost