"Android Layout" set Android:gravity and Android:layout_gravity properties in your program

Source: Internet
Author: User

The LinearLayout has two very similar properties:

Android:gravity and Android:layout_gravity.

The difference between them is:

The Android:gravity property is the qualification for the content in the view. For example, a button above the text. You can set the text relative to the view's left, right, and so on.

Android:layout_gravity is used to set the location of the view relative to the parent view. For example, a button in the LinearLayout, you want to put the button in the LinearLayout left, on the right and so on can be set by this property.

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 principle is a bit similar to Android:paddingleft and Android:layout_marginleft. If both properties are set at the same time on the button.

Android:paddingleft= the content set on the 30px button is 30 pixels from the left edge of the button
Android:layout_marginleft= "30px" The entire button is set from the left side of the content 30 pixels


To get back to the point below, we can set the android:gravity= "center" to let the text in the EditText be centered in the EditText component, while we set the android:layout_gravity= of EditText " Right "to let the EditText component appear in the LinearLayout. Look at the effect:

As we can see, in EditText, where the text is already centered, and the EditText component itself is aligned to the right side of the linearlayout.

Attach the layout file:

[XHTML]View PlainCopy
  1. <LinearLayout
  2. 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. <EditText
  7. android:layout_width="wrap_content"
  8. android:gravity="center"
  9. android:layout_height="wrap_content"
  10. android:text="one"
  11. android:layout_gravity="right"/>
  12. </linearlayout>

"Android Layout" set Android:gravity and Android:layout_gravity properties in your program

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.