Gravity and Layout_gravity of Android layout properties

Source: Internet
Author: User

Gravity is used to describe the location of the current view's contents in the view.

For example     <Button        android:layout_width= "wrap_content"         android:layout_height= "Wrap_content"         android:gravity= "bottom"         android:text= "Hello"/> Then Hello will appear at the bottom of the button.   If the current view is a view group, for example <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"     xmlns:tools= "Http://schemas.android.com/tools"     android:orientation= "Horizontal"     Android:layout_width= "Match_parent"     android:layout_height= "match_parent"     android:gravity = "Bottom" >     <Button        android:layout_width= "Wrap_content"         android:layout_height= "wrap_content"         android:text= "Hello"/>  </LinearLayout> that button will appear at the bottom of layout. For this example, the bottom of the screen.   If you change the width and height of the Button to match_parent that for     <Button        Android:laYout_width= "Match_parent"         android:layout_height= "match_parent"         android:gravity= "Bottom"         android:text= "Hello"/>button is as large as layout, and Hello is at the bottom of the button. For <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"     xmlns:tools= "http/ Schemas.android.com/tools "    android:orientation=" Horizontal "    android:layout_width=" Match_ Parent "    android:layout_height=" match_parent "    android:gravity=" Bottom ">     <Button        android:layout_width= "match_parent"         Android: layout_height= "Match_parent"         android:text= "Hello"/> </linearlayout> Because the button is now as large as layouty, the android:gravity in LinearLayout does not actually show the effect. In addition, this property has no recursive effect.  gravity is the position in the view (or view group) that controls its contents or contains views. Layout_gravity is the location of the view in its parent container view group. This property is only valid when the parent container is linearlayout and framelayout。 If you want to implement a button that appears at the bottom of linearlayout (the second example above), use Layout_gravity to write <linearlayout xmlns:android= "http// Schemas.android.com/apk/res/android "    xmlns:tools=" Http://schemas.android.com/tools "    android:orientation= "Horizontal"     android:layout_width= "match_parent"     Android:layout_ height= "Match_parent"      >     <Button        Android:layout_ gravity= "Bottom"         android:layout_width= "wrap_content"         Android: layout_height= "Wrap_content"         android:text= "Hello"/> </linearlayout>  Just delete the android:gravity= "bottom" in the LinearLayout and then button to add android:layout_gravity= "bottom"   if the android in the example above : orientation= "Horizontal" is changed to vertical, then the button position is above. This is because for linearlayout, if it is horizontal, the vertical position can only be controlled by the properties of the upper and lower structures such as top,center,bottom, and if it is vertical, it can only control the horizontal position.   Like gravity, this property has no actual effect if the view is the same size as the parent container view group. In fact, through Wrap_content and Match_p,Arent, this situation often occurs. So often found gravity and layout_gravity invalid situation. Check to see if the parent-child view is the same size.

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.