Possible Duplicate:
Android - gravity and layout_gravity
I have been working on android xml's. I have used android:layout_gravity="center"
to align components in specific position.
Now a days, when I was working on a Dialog like Activity, I came across android:gravity="center"
. What I found there was, gravity
was used to align its child
to a specific position.
I would share an example of what it is,
android:gravity="center"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >
<EditText
android:id="@+id/enterNumberEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter No." >
<requestFocus />
</EditText>
</LinearLayout>
It aligns the child of LinearLayout
in center.
android:layout_gravity="center"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:background="@drawable/dialog_background" >
</LinearLayout>
It aligns the LinearLayout
itself in center.
Now my questions are:
- If I am right in these assumptions, that whether
gravity
is for child components andlayout_gravity
is for parent component. - When to choose
gravity
overlayout_gravity
.
I have found this much explanation working on xml's so far. I would like to hear much better explanation of it. Let me know, if I am wrong somewhere
gravity
aligns the content of a view,layout_gravity
aligns a view within its parent. – Ammunition