An example of an Android app that uses LinearLayout to center the layout is explained _android

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.