Android gravity and layout_ The difference between gravity

Both are properties that set the alignment. The internal attribute values are the same.

You can also understand the meaning according to the English meaning. Such as center_ Horizontal indicates that the position in the horizontal direction is in the middle.

Gravity sets the alignment of its internal elements. For example, a textview is used to set the alignment of internal text. If it is a ViewGroup component such as LinearLayout, set the alignment of its internal view components.

layout_ Gravity sets the alignment of itself equivalent to the parent container. For example, a textview sets layout_ Gravity property, which indicates the alignment of the textview relative to the parent container.

Specific reference is as follows

①TextView

[Code]

[tip] it's convenient to observe here. We set a background color for textview. Fixed width set

[note] if you want to use the gravity attribute, the layout of this component_ Width and layout_ Height cannot be set to wrap_ content。 The gravity attribute set at this time has no effect, because the component is wrapped with content. No matter what is set, it cannot be changed

[effect]

②LinearLayout

[Code]

[prompt] at this time, the gravity of the external ViewGroup component is set to center, and the internal component textview is in the middle of the external LinearLayout. If the gravity property is not set in textview, the text is at the beginning

[effect]

For layout_ Gravity is not a property that can be set under any circumstances (such as relativelayout), and the effect will be different in different viewgroups (such as LinearLayout).

Specific examples are as follows

①LinearLayout

[effect]

[prompt] layout_ The center value of gravity does not make textview in the middle of LinearLayout, which is related to the positioning direction of the orientation of LinearLayout.

When it is set to horizontal, the components in LinearLayout are arranged horizontally_ When the gravity property is the center value, the component is in the middle of the vertical direction. Similarly, when it is set to vertical, the effect is as follows

② FrameLayout

[Code]

[effect]

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
分享
二维码
< <上一篇
下一篇>>