Android: Gravity/Android: layout_gravity, Android: padding/Android: layout_margin attribute Differentiation

Source: Internet
Author: User

Android: Gravity/Android: layout_gravityDifferences:

Android: gravity is used to set the position of the content in the view relative to the view. For example, set the text in the button to the left and center of the view. Android: layout_gravity is used to set the relative position of the view and its parent view. For example, you can set the relative position of the button in layout: center the screen and center horizontally.

In practice, it is found that Android: gravity and Android: layout_gravity are used simultaneously:

< Textview
Android: ID = "@ + ID/text_displayed"
Android: gravity = "Center" // Set the center position of the text in textview
Android: visibility = "Invisible"
Android: layout_width = "Fill_parent"
Android: layout_height = "Wrap_content"
Android: layout_gravity = "Center" // Set the textview position (framelayout): center the screen
/> Java Code Setting Android: gravity in is extremely simple, and the setgravity function is done: // This is equivalent to the Android: gravity attribute in the layout file.
Button. Setgravity (Gravity. center );

Android: padding/Android: layout_marginDifferences:

Padding describes the problem from the perspective of the parent view. It specifies the distance between the content and the parent view. Margin describes the problem from the perspective of itself and specifies the distance between itself and other views (up and down). If there is only one view at the same level, the effect is basically the same as that of padding.

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.