Differences between Android: layout_gravity and Android: gravity

Source: Internet
Author: User

1. First, let's take a look at the differences between Android: layout_gravity and Android: gravity.

Android: gravity:

This is for the elements in the control, used to control the display position of the elements in the control. For example, in a button control, set the following two attributes,

Android: gravity = "Left" and Android: text = "Submit". The text "Submit" on the button is located at the left of the button.

Android: layout_gravity:

This control is used to control the position of the control in the parent control containing the control. Similarly, when we set the Android: layout_gravity = "Left" attribute in the button control, it indicates that the button will be located at the left of the interface.

2. attribute values:

Optional values of these two attributes include top, bottom, left, right, center_vertical, fill_vertical, center_horizontal, fill_horizontal, center, fill, and clip_vertical.

An attribute can contain multiple values separated by "|. Its meaning is as follows:

Top Place the object on the top of the container without changing its size.
Bottom Place the object at the bottom of the container without changing its size.
Left Place the object on the left of its container without changing its size.
Right Place the object on the right of its container without changing its size.
Center_vertical Center the object vertically without changing its size.
Vertical Alignment: center alignment in the vertical direction.
Fill_vertical When necessary, increase the vertical size of the object to be fully filled with its container.
Vertical Filling
Center_horizontal Center the object horizontally without changing its size.
Horizontal Alignment: Align horizontally to center
Fill_horizontal When necessary, increase the horizontal size of the object to be fully filled with its container.
Horizontal Filling
Center Center an object vertically without changing its size.
Fill When necessary, increase the horizontal and vertical size of the object to fully fill the container.
Clip_vertical

Additional options are used to cut the content at the top and/or bottom of the object based on the container edge. based on its vertical alignment settings: when the top is aligned, the bottom is cut; when the bottom is aligned, the top is cut; in addition, the top and the bottom are cut.

Vertical crop

Clip_horizontal

Additional options are used to cut the content on the left and/or right of the object based on the container edge. based on its horizontal alignment settings: When alignment is left, the right side is cut; When alignment is right, the left side is cut; in addition, the left side and the right side are cut.

Horizontal crop


Let's take a look at the two attribute values: center_vertical and center_horizontal. center_vertical is to align the object vertically, that is, select the center position from top to bottom; center_horizontal is to align the object horizontally, that is, to select the center position from left to right.

3. Special Cases

When we adopt the linearlayout layout, note the following special situations:

(1) When Android: Orientation = "vertical", Android: layout_gravity takes effect only in the horizontal direction and the vertical direction does not. That is, left, right, and center_horizontal take effect.

(2) When Android: Orientation = "horizontal", Android: layout_gravity takes effect only in the vertical direction and the horizontal direction does not. That is, top, bottom, and center_vertical take effect.

The Chinese meaning of gravity is "Center of Gravity", indicating the horizontal and vertical parking position of the view.

Android: gravity: For the view control, it is used to set the position where the view text should be displayed. The default value is left.

Android: layout_gravity: relative to the parent element containing the modified element, set the location of the element in the parent element.

For example, textview: Android: layout_gravity indicates the position of textview on the interface. Android: gravity indicates the position of textview text in textview. The default value is left.

<? 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>

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.