Android Properties Gravity, layout_gravity, padding, Layout_margin and a demo about Gravity/layout_gravity

Source: Internet
Author: User

1. android:gravity

The relative position of the interior -----used to set the view's relative position within the view (left, right, top, bottom, center ..., not a specific value).

(The official explanation is:

  • Gravity---Specifies how to place the content of a object, both on the X-and y-axis, within the object itself.
  • Meaning of the value center---Place the object in the center of its container in both the vertical and horizontal axis, not changing its s Ize.)

2. android:layout_gravity

the relative position of the external -----used to set the view's relative position within its parent view (left, right, top, bottom, center ..., not a specific value).

(The official explanation is:

  • Layout_gravity---Standard gravity constant, a child can supply to its parent. Defines how to place the view, both it x-and y-axis, within its parent view group.
  • Meaning of the value center---Place the object in the center of its container in both the vertical and horizontal axis, not changing its s Ize.)

3. android:padding

the absolute position of the internal-----used to set the distance between the view's content and the view's border or four borders (which is a specific value).

The size of a view includes both the content of the view and the padding value.

4. Android:layout_margin

the absolute position of the external -----used to set the value of the distance between the view and a border of its parent view or four borders (a specific value).

But please note:
If you want a child control to be centered horizontally and vertically in its parent control, you typically think of having the parent control use Relativelayout and setting the child control's property android:layout_centerinparent= "true". However, if you also require that the width of the child control be proportional to the width of the parent control (for example, half the width of the parent control), then only consider having the parent control use the horizontal LinearLayout and set its property android:weightsum, and the child control's width is set to 0DP. and sets the property android:layout_weight equal to half the value of the parent control's Android:weightsum property. At this point, the child controls are centered in both horizontal and vertical two directions within the parent control, and we might think of two workarounds:

    1. Set the properties of the parent control linearlayout android:gravity= "center"
    2. Sets the properties of the child control Android:layout_gravity= "center".

However, in actual use, it will be found that the former is valid, the latter is invalid. That is, only the properties of the LinearLayout are set android:gravity= "center" to meet the location requirements.

The following shows the effects of the two layout styles, respectively:

1. Set the android:gravity= "center" of the parent control LinearLayout

The layout file is as follows:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= " Fill_parent "    android:layout_height=" Fill_parent "    android:orientation=" Horizontal "     android:gravity= "center"    android:weightsum= "1.0" >    <button        Android:id= "@+id/button1"        android:layout_width= "0DP"        android:layout_height = "Wrap_content"        android:layout_weight= "0.5"        android:text= "button"/> </LinearLayout>

The vertical and horizontal display effects of this layout are as follows:

This shows that the above layout has successfully achieved our ideas.

2. Set the android:layout_gravity= "center" of the child controls.

The layout file is as follows:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= " Fill_parent "    android:layout_height=" Fill_parent "    android:orientation=" Horizontal "     Android:weightsum= "1.0" >    <button        android:id= "@+id/button1"         Android:layout_width= "0DP"        android:layout_height= "Wrap_content"        android:layout _weight= "0.5"        android:layout_gravity= "center"        android:text= "button"/> </LinearLayout>

The vertical and horizontal display of the layout is as follows:

As you can see, the width of the child control is indeed half the width of the parent control, but the location of the child control does not meet the requirements. Setting the layout_gravity= "Center" property of a child control can only center the child control vertically, but not horizontally, but at the far left of the parent control. This program can not meet our needs, I can not think of the reason for the moment, it is estimated that the understanding of the API is not deep enough, I hope to know the reasons for the friend to give some advice, thank you!

Android Properties Gravity, layout_gravity, padding, Layout_margin and a demo about Gravity/layout_gravity

Related Article

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.