Gravity and Layout_gravity properties in Android layout

Source: Internet
Author: User

Gravity This English word is the meaning of the center of gravity, here means the docking position.

The difference between android:gravity and android:layout_gravity:

Android:gravity is the position of the contents of the view relative to the view, such as setting the button inside the text relative to the left and middle position of the view. (You can also add the layout properties to set the location of components in layout).

Android:layout_gravity is used to set the position of the view relative to the parent view, such as setting the relative position of the button in layout: The screen is centered, horizontally centered, and so on.

That is, android:gravity is used to set the alignment of content in view relative to the view component, while android:layout_gravity is used to set the alignment of the view component with respect to the container. The more straightforward point is that android:gravity is only valid for what is inside the component, and android:layout_gravity is only valid for the component itself.


android:layout_gravity are only valid in LinearLayout and framelayout:

1, for LinearLayout:

When android:orientation= "vertical" (vertical), only the horizontal setting will work, and the vertical setting will not work. That is: The left,right,center_horizontal is in force.

When android:orientation= "horizontal" (horizontal), only the vertical setting will work, and the horizontal setting will not work. That is: The top,bottom,center_vertical is in force.

2, for framelayout:any android:layout_gravity property is valid, which makes it very easy to implement the layout of the component. Look at an example:

The entire layout uses linearlayout, where the android:gravity= "center_vertical" attribute is added to the linearlayout so that the button is in the horizontal middle of the screen. because LinearLayout's android:orientation= "vertical", the button can only use Android:layout_gravity properties of only three, namely: Left,right, Center_horizontal, the android:layout_gravity= "right" is used here, so the button is on the left. then see the button in the width of the Text,button I deliberately extended, so that you can clearly see the text is on the bottom of the button and the right, so the button android:gravity= "Right|bottom", Properties can be multiple, with the ' | ' Separate.
<span style= "Font-family:microsoft Yahei;" ><?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical"    android:gravity= "center_vertical" >        <button         android:layout_width= "150px "            android:layout_height=" wrap_content "           android:text=" test "            android:gravity=" Right|bottom        " Android:layout_gravity= "Right"/></linearlayout></span>

to set the location of the component through Java code:

Setgravity () corresponds to Android:gravity

Gravity property corresponds to Android:layout_gravity

The following code implements the effect of the above layout file implementation

<span style= "Font-family:microsoft Yahei;" >button button = New button (this), Button.settext ("test"),//sets the text position in the button, equivalent to android:gravity= "right" in the button layout property. |bottom "Button.setgravity (gravity.bottom| Gravity.right); LinearLayout layout = new LinearLayout (this); Layout.setorientation (linearlayout.vertical); LinearLayout must set the direction, otherwise cannot see the effect//deliberately longer button length, here 200 of the unit is pxlinearlayout.layoutparams buttonparams = new  Linearlayout.layoutparams (layoutparams.wrap_content);   Equivalent to android:layout_gravity=rightbuttonparams.gravity = gravity.right in the button layout property; Equivalent to these two sentences button.setlayoutparams (buttonparams);//Layout.addview (button); Layout.addview (Button, Buttonparam  s); Sets the location of the button, equivalent to the android:gravity= "Center_vertical" layout.setgravity (gravity.center_vertical) in the LinearLayout layout properties            ; Setcontentview (layout); </span> 
Reference: http://blog.csdn.net/feng88724/article/details/6333809 http://www.cnblogs.com/ghj1976/archive/2011/04/26/2029535.html


Gravity and Layout_gravity properties in Android layout

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.