Gravity and Layout_gravity of Android layout properties

Source: Internet
Author: User

1. Gravity is used to describe the location of the contents of the current view in the view.

Gravity is the position of the view (or view group) that controls its contents or contains views

2. Layout_gravity is the location of the view in its parent container view group.

This property is only valid when the parent container is linearlayout and framelayout

Gravity's Chinese meaning is "center of gravity", that is, the view horizontal and vertical docking position

Android:gravity: Is the view control itself, is used to set the view itself where the content should be displayed in the view where the default value is left.

Android:layout_gravity: Is relative to the parent element that contains the element being changed, and sets where the element is located in the parent element

For example, TextView:android:layout_gravity represents the position of TextView on the interface, android:gravity indicates where the TextView text is in TextView, and the default value is the left.

Basic option values:

Value Description
Top Put the object at the top of it container, not changing its size. Places the object at the top of its container without changing its size.
Bottom Put the object at the bottom of it container, not changing its size. Places the object at the bottom of its container, without changing its size.
Left Put the object at the left edge of it container, not changing its size. Places the object on the left side of its container without changing its size.
Right Put the object at the right edge of its container, not changing its size. Places the object on the right side of its container without changing its size.
Center_vertical Place object in the vertical center of its container, not changing its size. Centers the object vertically without changing its size. Vertical alignment: Center-aligned vertically.
Fill_vertical Grow the vertical size of the object if needed so it completely fills its container. When necessary, increase the vertical size of the object to fully fill its container. Vertical fill
Center_horizontal Place object in the horizontal center of its container, not changing its size. Centers the object horizontally, without changing its size. Horizontal alignment: Center aligned horizontally
Fill_horizontal Grow the horizontal size of the object if needed so it completely fills its container. When necessary, increase the horizontal size of the object to fully fill its container. Fill horizontally
Center Place the object in the center of its container in both the vertical and horizontal axis, not changing its size. Centers the object horizontally, without changing its size.
Fill Grow the horizontal and vertical size of the object if needed so it completely fills it container. This is the default. When necessary, increase the cross-portrait size of the object to fully fill its container.
Clip_vertical Additional option that can is set to has the top and/or bottom edges of the child clipped to its container ' s bounds. The clip is based on the vertical gravity:a top gravity clips The bottom edge, a bottom gravity clips the top edge, and N Either clips both edges.

Additional options for cutting the contents of the top and/or bottom of the object according to the edges of the container. Clipping is based on its vertical alignment settings: When the top is aligned, the bottom is clipped, the top is clipped at the bottom, and the top and bottom are cut apart.

Crop vertically

Clip_horizontal Additional option that can is set to has the left and/or right edges of the child clipped to its container ' s bounds. The clip is based on the horizontal gravity:a left gravity clips the right edge, a right gravity clips the left edge, and Neither clips both edges.

An additional option to cut the contents of the left and/or right of the object according to the edges of the container. Clipping is based on its horizontal alignment settings: Cut to the right when left aligned, cut to the left when aligned to the right, and cut to the left and right.

Horizontal Orientation Clipping

The following example

View Plain
    1. <?xmlversion= "1.0" encoding= "Utf-8"?>
    2. <linearlayoutxmlns:android= "Http://schemas.android.com/apk/res/android"
    3. android:orientation= "Vertical"
    4. Android:layout_width= "Fill_parent"
    5. android:layout_height= "Fill_parent"
    6. >
    7. <textview
    8. Android:layout_width= "100dip"
    9. android:layout_height= "100dip"
    10. Android:layout_gravity= "Bottom|center_horizontal"
    11. Android:gravity= "Center|bottom"
    12. Android:background= "#00FF00"
    13. android:text= "@string/textview"
    14. />
    15. <button
    16. Android:layout_width= "100dip"
    17. android:layout_height= "100dip"
    18. Android:layout_gravity= "Bottom|left"
    19. Android:gravity= "Left|top"
    20. Android:background= "#FF0000"
    21. android:text= "@string/button"
    22. />
    23. </LinearLayout>

Note that TextView is not displayed in the center of the interface as we set the Android:layout_gravity property, and the button is not displayed at the bottom left of the interface. This is because we have set the Android:orientation property of LinearLayout to "vertical". For LinearLayout if the android:orientation= "vertical" is set, then the Android:layout_gravity setting is only in the horizontal direction, as the TextView appears in the horizontal center of the screen, The button appears at the far left of the horizontal direction, and if you set android:orientation= "Horizontal", the Android:layout_gravity property only takes effect in the vertical direction.

Gravity and Layout_gravity of Android layout properties

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.