Differences between the android:layout_gravity and Android:gravity properties

Source: Internet
Author: User

Gravity's Chinese meaning is "center of gravity", that is, the view horizontal and vertical docking position

Android:gravity: Is the view control itself, is the text used to set the view itself should be displayed in the view where the default value is the left

Android:layout_gravity: Is relative to the parent element that contains the element being changed, and sets where the element is located in the parent element

For example, TextView:android:layout_gravity represents the position of TextView on the interface, android:gravity indicates where TextView text is in TextView, and the default value is the left

The following example

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="100dip"
  9. android:layout_height="100dip"
  10. android:layout_gravity="Bottom|center_horizontal"
  11. android:gravity="Center|bottom"
  12. android:background="#00FF00"
  13. android:text="@string/textview"
  14. />
  15. <Button
  16. android:layout_width="100dip"
  17. android:layout_height="100dip"
  18. android:layout_gravity="Bottom|left"
  19. android:gravity="Left|top"
  20. android:background="#FF0000"
  21. android:text="@string/button"
  22. />
  23. </linearlayout>

Note that TextView is not displayed in the center of the interface as we set the Android:layout_gravity property, and the button is not displayed at the bottom left of the interface. This is because we have set the Android:orientation property of LinearLayout to "vertical". For LinearLayout if the android:orientation= "vertical" is set, then the Android:layout_gravity setting is only in the horizontal direction, as the TextView appears in the horizontal center of the screen, The button appears at the far left of the horizontal direction, and if you set android:orientation= "Horizontal", the Android:layout_gravity property only takes effect in the vertical direction.

Transferred from: http://blog.csdn.net/dekunchenivan/article/details/6718678

Differences between the android:layout_gravity and Android:gravity attributes (RPM)

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.