android:layout_ Gravity and Android: the difference between gravity and Android

1. Let's first look at Android: layout_ Gravity and Android: the difference between gravity and Android.

android:gravity:

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

Android: gravity = "left" and Android: text = "submit", the text "submit" on the button will be located on the left of the button.

android:layout_ gravity:

For the control itself, this is used to control the position of the control in the parent control containing the control. Similarly, when we set Android: Layout in the button control_ Gravity = "left" attribute indicates that the button button will be located on the left of the interface.

2. Attribute value:

The optional values of these two attributes are: top, bottom, left, right and center_ vertical、fill_ vertical、center_ horizontal、fill_ horizontal、center、fill、clip_ vertical。

An attribute can contain multiple values, which should be separated by "|". It has the following meanings:

Let's mainly look at the center_ Vertical and center_ Horizontal two attribute values,

center_ Vertical means to align the object in the center in the vertical direction, that is, select the middle position from top to bottom; center_ Horizontal means to center the object horizontally, that is, select the middle position from left to right.

3. Special circumstances

When we adopt LinearLayout layout, we need to pay attention to the following special situations:

(1) When Android: orientation = "vertical", Android: Layout_ Gravity only works in the horizontal direction, but not in the vertical direction. Left, right, center_ Horizontal is effective.

(2) When Android: orientation = "horizontal", Android: Layout_ Gravity only works in the vertical direction, but not in the horizontal direction. Top, bottom, center_ Vertical is effective.

The following is an example: (this example comes from: http://blog.csdn.net/dekunchenivan/article/details/6718678 )

The effect is shown in the figure below:

@H_ 502_ 164@

In textview, we set Android: layout_ Gravity = "bottom | center _horizontal", but the textview is not displayed in the lower center of the screen, indicating that there is only center_ The horizontal attribute works because we use the LinearLayout layout and its Android: orientation = "vertical". Only the settings in the horizontal direction will work, and other directions will fail. Similarly, the button is the same.

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>