The difference between android:layout_gravity and android:gravity

Source: Internet
Author: User

1. First look at the difference between the use of android:layout_gravity and android:gravity.

Android:gravity:

This is for the elements in the control, to control where the element is displayed in the control. For example, set the following two properties in a button control

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 is for the control itself, which controls the position of the control in the parent control that contains the control. Similarly, when we set the android:layout_gravity= "left" property in the button control, it means that the 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


We mainly look at the center_vertical and center_horizontal two attribute values, center_vertical refers to the object in the vertical direction of alignment, that is, in the direction from the top to the bottom of the selection of the middle position; center_ Horizontal refers to centering the object horizontally, that is, selecting the middle position in the left-to-right direction.

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.

Here is an example: (This example is from: http://blog.csdn.net/dekunchenivan/article/details/6718678)

<?xml version="1.0"encoding="Utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="Vertical"Android:layout_width="fill_parent"Android:layout_height="fill_parent"> <TextView android:layout_width="100dip"Android:layout_height="100dip"android:layout_gravity="Bottom|center_horizontal"android:gravity="Center|bottom"Android:background="#00FF00"Android:text="@string/textview"/> <Button android:layout_width="100dip"Android:layout_height="100dip"android:layout_gravity="Bottom|left"android:gravity="Left|top"Android:background="#FF0000"Android:text="@string/button"/> </LinearLayout>

Its effect

In TextView, we set the android:layout_gravity="Bottom|center_horizontal", but the TextView is not displayed in the center of the screen, indicating that only the Center_ The horizontal property works because we use the LinearLayout layout, and its android:orientation= "vertical", only the horizontal setting will work, and the other directions will fail. Similarly, the button is the same.

Reference: http://blog.csdn.net/shakespeare001/article/details/7843460

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.