Gravity and layout_gravity differences in "finishing" Android

Source: Internet
Author: User


Background

In Android, you want to set the horizontal alignment of a button, you are exhausted:

How to set the center alignment of a button if the Tablelayout layout has been resolved in ADT it is now necessary to figure out what the difference is between gravity and layout_gravity.

1. Reference:

Android–gravity and Layout_gravity

The difference between gravity and layout_gravity in Android

The explanations in this article can be summed up as:

      • Android:gravity: Represents the current view, that is, the control, the inner thing, the alignment
        • Button inside the TableRow
          • Right alignment:
            • Code:
<tablerow    android:layout_width= "fill_parent"     android:layout_height= "Fill_parent"    android: Gravity= "Right" >    <button        android:layout_width= "fill_parent"         android:layout_height= "Fill_ Parent "        android:gravity=" center "        android:layout_gravity=" center "        android:onclick=" Preformdownload "        android:text=" @string/btn_download "/></tablerow>
            • Effect:
          • Center alignment:
            • Code:
<tablerow    android:layout_width= "fill_parent"     android:layout_height= "Fill_parent"    android: gravity= "center" >     <button         android:layout_width= "fill_parent"          android:layout_height= "Fill_ Parent "         android:gravity=" center "         android:layout_gravity=" center "         android:onclick=" Preformdownload "         android:text=" @string/btn_download "/> </TableRow>
          • Effect:
        • Text (text) inside the button:
          • Perpendicular: Top Top
            • Code:
     <button         android:layout_width= "fill_parent"         android:layout_height= "fill_parent"         android:layout_ gravity= "Bottom"         android:gravity= "top"         android:onclick= "preformdownload"         android:text= "@string/ Btn_download "/>
            • Effect:
          • Vertical: Center Alignment:
            • Code:
     <button         android:layout_width= "fill_parent"         android:layout_height= "fill_parent"         android:layout_ gravity= "Bottom"         android:gravity= "center_vertical"         android:onclick= "Preformdownload"         android:text= "@string/btn_download"/>
            • Effect:
      • Android:layout_gravity: Represents the alignment of the current view, that is, the control, itself, in the display range assigned to the control at the parent level
        • EditText (within the linearlayout of the parent level)
          • EditText Center Alignment:
            • Code:
<linearlayout     xmlns:android= "http://schemas.android.com/apk/res/android"     android:orientation= " Vertical "     android:layout_width=" fill_parent "     android:layout_height=" fill_parent ">     <edittext        android:layout_width= "wrap_content"        android:layout_height= "wrap_content"        android:layout_gravity= " Center "        android:gravity=" center "        android:text=" one "/></linearlayout>
            • Effect:
          • EditText Right-aligned:
            • Code:
<linearlayout     xmlns:android= "http://schemas.android.com/apk/res/android"     android:orientation= " Vertical "     android:layout_width=" fill_parent "     android:layout_height=" fill_parent ">     <edittext        android:layout_width= "wrap_content"        android:layout_height= "wrap_content"        android:layout_gravity= " Right "        android:gravity=" center "        android:text=" one "/></linearlayout>
          • Effect:

Summary

    • android:gravity : Represents the current view, that is, the control, the inner thing, the alignment
      • The common ones are:
        • button in the TableRow
        • EditText (internal) text
        • The text of the Button (inside)
    • android:layout_gravity: Represents the alignment of the current view, which is the control itself , that is within the display range assigned by the parent level control to the current child control.
      • The common ones are:
        • The alignment of the current edittext (within the display range assigned to it by the parent level linelayout)
        • The alignment of the current button (within the display range assigned to it by the parent level TableRow) Here, it is important to note that many times, changing the layout_gravity in the button often does not see the effect of the change, because its display range and position, has been determined by the gravity of the TableRow at the parent level.

In short, take the code, try more, it is easy to understand.

Gravity and layout_gravity differences in "finishing" Android

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.