Android:layout_gravity and android:gravity attribute differences

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 side

Android:layout_gravity: Sets the position of the element in the parent element relative to the parent element that includes the element being changed


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


For example, the following examples

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



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 the linearlayout hypothesis setting android:orientation= "vertical", then the android:layout_gravity settings are only in the horizontal direction, such as TextView displayed in the horizontal center of the screen, The button appears at the far left of the horizontal direction, assuming that the android:orientation= "horizontal" is set, then the Android:layout_gravity property only takes effect in the vertical direction.



Android:layout_gravity and android:gravity attribute differences

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.