The difference between android:layout_gravity and android:gravity

Source: Internet
Author: User

The difference between the use of 1.android:layout_gravity and android:gravity.

Android:gravity:

This controls the display position of the elements in the control. For example, in a button control, the following settings

Android:gravity= "left" and android:text= "submit", then the text "Submit" on the button will be on the right side of the button.

Android:layout_gravity:

This controls the position of the control itself in its parent control. Similarly, when we set the button in the buttons control

Android:layout_gravity= the "left" property, this button will be located at the right-hand side of the interface.

2. Attribute values:

The optional values for these two properties are: Top, bottom, left, right, center_vertical, fill_vertical, Center_horizontal, fill_horizontal, center, fill, Clip_vertical.

A property can contain multiple values and requires the "|" Separate. It has the following meanings:

Top Places the object at the top of its container without changing its size.
Bottom Places the object at the bottom of its container, without changing its size.
Left Places the object on the left side of its container without changing its size.
Right Places the object on the right side of its container without changing its size.
Center_vertical Centers the object vertically without changing its size.
Vertical alignment: Center-aligned vertically.
Fill_vertical When necessary, increase the vertical size of the object to fully fill its container.
Vertical fill
Center_horizontal Centers the object horizontally, without changing its size.
Horizontal alignment: Center aligned horizontally
Fill_horizontal When necessary, increase the horizontal size of the object to fully fill its container.
Fill horizontally
Center Centers the object horizontally, without changing its size.
Fill When necessary, increase the cross-portrait size of the object to fully fill its container.
Clip_vertical

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

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

3. Special Circumstances

When we adopt the LinearLayout layout, we should pay attention to the following special cases:

(1) When android:orientation= "vertical", the android:layout_gravity only works horizontally, and the vertical setting does not work. That is: The left,right,center_horizontal is in force.

(2) when the android:orientation= "horizontal", android:layout_gravity only the vertical direction of the setting to function, the horizontal direction of the setting does not work. That is: The top,bottom,center_vertical is in force.

The difference between android:layout_gravity and android: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.