2
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frameLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_gradient" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center" >

        <ImageButton
            android:id="@+id/buttonLog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/log"
            android:onClick="log" />

    </RelativeLayout>

</FrameLayout>

I was expecting my button to appear in the center of the screen. However, it appears on the TOP center of the screen (that is, the button is center horizontally, but not vertically).

It seems to me that the RelativeLayout is behaving like it was defined with "wrap_content" instead of "fill_parent".

The funny thing is that, if I give an actual value to my RelativeLayout height property (android:layout_height), like:

<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:gravity="center" >

Then the button behaves correctly (i.e. the button is also centred vertically). But I don`t want to use actual values. I want to use fill_parent! Why doesn't it work with "fill_parent" ??

Does anybody know what's going on?

Thank you in advance!

2 Answers2

4

RelativeLayout requires you to specify the position of the elements in the Layout. I don't see any layout_below or layout_toLeftOf tags. Gravity works on LinearLayouts. In general, LinearLayouts are easier to work with, and they scale much better to different screen sizes. I suggest you replace the RelativeLayout by a LinearLayout, and also the FrameLayout by a LinearLayout. You use a FrameLayout typically if you want to use multiple overlapping layouts, which you don't do.

I recommend you read up on using layouts in the Android sdk reference documentation, like here: http://bit.ly/djmnn7

Christine
  • 5,617
  • 4
  • 38
  • 61
  • Humm, I have used RelativeLayouts before without those attributes. But anyway, I will take your suggestion into consideration from now on. Thanks! (+1). – Bitcoin Cash - ADA enthusiast Jun 01 '12 at 22:33
  • Everything you can do with RelativeLayouts you can also do with LinearLayouts, except that it gets complicated when you have elaborate layouts. The advantage of LL is that it always scales well on all devices. Use nested LL's with proper weights to get the UI layout you want. Have a look at the reference documentation for layout width, height and weight. – Christine Jun 04 '12 at 01:02
1

You specified fill_parent for both the layout_width and layout_height of your RelativeLayout, therefore it fills up it's parent view. By default, a relative layout arranges it's children to the top-left corner, regardless you use fill_parent for the size.

You should achieve the desired aspect by taking advantage of the RelativeLayout's own attribute set, which helps you arrange the child views relatively to each other or to their parent:

<ImageButton
    android:id="@+id/buttonLog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="@drawable/log"
    android:onClick="log" />

Using the android:layout_centerInParent you can achieve this. This attribute if set true, centers this child horizontally and vertically within its parent.

rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • Hi rekaszeru. Thanks for replying. I understand that the relative layout will arrange its children starting from the top-left corner by default. However, that`s exactly why I have set the android:gravity="center" property in that RelativeLayout, so its children get centred (but that's not what happens). Can you explain why the center attribute doesn't do anything here? – Bitcoin Cash - ADA enthusiast Jun 01 '12 at 05:33
  • 1
    Please see this explanation: http://stackoverflow.com/a/6819801/506879 Also take a look at the `RelativeLayout's` attributes: http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html There is no `gravity` attribute. – rekaszeru Jun 01 '12 at 06:51
  • 1
    Wow! No gravity attribute... I actually used the code completion of Eclipse for that, which indeed showed me the gravity attribute for the relative layout when I hit (Ctrl + space). Once that completion is made by Google (and it was recently incorporated in the Android plugin), the least I was expecting is that it was correct... I guess I can't rely on Eclipse's suggestions then. I will always check the documentation on the website. I have to say that this is VERY annoying though... Thanks for your help rekaszeru! – Bitcoin Cash - ADA enthusiast Jun 01 '12 at 22:30