<?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!
You specified
fill_parentfor both thelayout_widthandlayout_heightof yourRelativeLayout, 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_parentfor the size.You should achieve the desired aspect by taking advantage of the
RelativeLayout'sown attribute set, which helps you arrange the child views relatively to each other or to their parent:Using the
android:layout_centerInParentyou can achieve this. This attribute if set true, centers this child horizontally and vertically within its parent.