It's easy to center your control horizontally or vertically, as long as you set the Android:gravity= center property to the top level of the control
Such as:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:orientation= "vertical"
android:gravity= "center"
android:background= "#000000"
android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
>
<imageview
android:id= "@+id/logo"
@ Drawable/logo "
android:layout_width=" wrap_content "
android:layout_height=" wrap_content "
/>"
</LinearLayout>
Such a ImageView control will obediently stay in the middle of your chosen area. There are a number of gravity property values that can be set horizontally or vertically in the center. You can view the documentation, or use the Eclipse hints feature for quick viewing.
Let's take a look at an example, if you want to implement such a layout, two buttons centered, what do you do?
Relatively easy to achieve is the linearlayout layout, of course, if replaced by Relativelayout can also be achieved.
Here simple to say with relativelayout layout how to achieve, first need to find the center point, then take this center point as the reference can be realized. Next look at the implementation of the LinearLayout layout that is easier to implement.
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "
android:layout_width=" match_parent "
android:layout_" height= "Match_parent"
android:gravity= "center" >
<button
android:layout_width= "Wrap_content"
android:layout_height= "wrap_content"
android:id= "@+id/start_server"
android:text= "Start"/>
<button
android:layout_width= "wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/stop_server"
android:text= "Stop"/>
</LinearLayout>
If the need changes now, only a button in the middle, then the implementation is very simple, you need to remove one of the button labels above, but some people to achieve
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "
android:layout_width=" match_parent "
android:layout_" height= "Match_parent" >
<button
android:layout_width= "Wrap_content"
Wrap_content "
android:id=" @+id/start_server "
android:layout_gravity=" center "
android:text=" Start " />
</LinearLayout>
This implementation is obviously not centered, although the button tag specifies the android:layout_gravity= "center" but seemingly the layout is ungrateful, why?
The reason is very simple, linearlayout can only have one direction, either vertical, or horizontal, completely will not lead layout_gravity this attribute of sentiment, so the above realization only such a result
If you change the layout to a horizontal direction
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" horizontal "
android:layout_width=" match_parent "
android:layout_" height= "Match_parent" >
<button
android:layout_width= "Wrap_content"
Wrap_content "
android:id=" @+id/start_server "
android:layout_gravity=" center "
android:text=" Start " />
</LinearLayout>
It's going to turn out like this.
According to the above theory can also be figured out, so android:layout_gravity= "center" this attribute although LinearLayout also have, but feint, the real role of its own attributes android:gravity= " Center ", of course, if you want to find out, why does not work, you can from linearlayout this layout source code look, here no longer repeat.