Layout_gravity and gravity

Source: Internet
Author: User

I believe that for beginners of Android, everyone has been confused by the two extremely similar attributes in layout.

Using a simple search tool, we can easily find the following answer:

Layout_gravity indicates the position of the component in the parent component.
Gravity indicates the position of the child component in the component.

It seems very simple ~)

It seems that everyone can understand it at a glance. What I want to talk about today is what seems to be clear at a glance.
Why is this simple principle always the same school found that in "some time", the property of layout_gravity is not good, and it has lost its due role.

As a result, the students started to use the search tool again and kept scolding: The layout made by Google is really bad!
The following is an example of a layout_gravity failure.

  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <! -- Android: gravity sets the text display position on the button, while android: layout_gravity sets the display position of the button in the layout. ->
  3. <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
  4. Android: orientation = "vertical"
  5. Android: layout_width = "fill_parent"
  6. Android: layout_height = "wrap_content">
  7. <Button android: layout_width = "250dip"
  8. Android: gravity = "right"
  9. Android: layout_height = "wrap_content"
  10. Android: text = "show me on the right"
  11. Android: layout_gravity = "right"/>;
  12. </LinearLayout>

Copy code

The effect of this Code is as follows :!

 

 

Then let's look at the following code:

  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <! -- Android: gravity sets the text display position on the button, while android: layout_gravity sets the display position of the button in the layout. ->
  3. <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
  4. Android: layout_width = "fill_parent"
  5. Android: layout_height = "wrap_content">
  6. <Button android: layout_width = "250dip"
  7. Android: gravity = "right"
  8. Android: layout_height = "wrap_content"
  9. Android: text = "show me on the right"
  10. Android: layout_gravity = "right"/>
  11. </LinearLayout>

Copy code

In this Code, we set android: layout_gravity = "right", but its display effect is as follows:

 

 

So what we mentioned above happened. "The layout made by Google is really bad !"

Where is the problem?
If you are careful, you will find that the LinearLayout on the outermost layer lacks the following attribute: androidrientation = "vertical"
Yes, this attribute is missing, causing the failure of android: layout_gravity = "right.
Because the default LinearLayout is: androidrientation = "horizontal ""

That is, android: layout_gravity = "right" takes effect only when LinearLayout is androidrientation = "vertical" as the parent layout.

Can this "Lecture" end now?No!

After reading the above content, some people may find that when the outer LinearLayout is androidrientation = "vertical", android: layout_gravity = "bottom" is invalid.

I believe everyone understands this.

Below we also need to make a simple summary:
Android: layout_gravity = "? "This takes effect only when it is set to horizontal. For example: left, right, center_horizontal, etc.

Android: layout_gravity = "? "This takes effect only when it is set to vertical. For example, top, bottom, and center_vertical;

There is a special center. Whether it is horizontal or vertical, it always works in one direction.

There are also a few other urgent issues: fill_vertical, fill_horizontal, fill, clip_vertical, clip_horizontal
I have never paid attention to these issues before. Is it a new version of the SDK? Decentralized alignment? Confused.
For more information

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.