Android:layout_weight's deep understanding

Source: Internet
Author: User

This article details the properties of Layout_weight in the Android layout, which is used to assign a property that belongs to space, and you can set his weight.

AD:

Recently wrote the demo, suddenly found layout_weight this attribute, found on the internet there are many interesting discussion about this attribute, but found a lot of information can not find a clear, so that their own combination of online data research, finally solved, write to share with you.

First look at the role of the Layout_weight property: It is used to assign a property that belongs to space, and you can set his weight. Many people do not know what the remaining space is a concept, let me say the rest of the space.

Look at the following code:

  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout 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. >
  7. <EditText
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:gravity="left"
  11. android:text="one"/>
  12. <EditText
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:gravity="center"
  16. android:layout_weight="1.0"
  17. android:text="/> "
  18. <EditText
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. android:gravity="right"
  22. android:text="three"/>
  23. </linearlayout>

The operating result is:

Look at the code above: only Button2 Use the Layout_weight property, and assign values to 1, and Button1 and Button3 do not set layout_weight this property, according to the API, they are 0 by default

Now I'm going to say, layout_weight the real meaning of this property: The Android system is based on the 3 button height layout_height values you set wrap_content, assigning you the height of their 3,

Then the rest of the screen space will be assigned to Button2, because only his weight value is 1, which is why Button2 accounted for such a large piece of space.

With the above understanding, we can have a clear understanding of the more puzzling effect of the layout_weight on the Internet.

Let's take a look at this piece of code:

  1. <? XML version= "1.0" encoding= "UTF-8"? >
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="Horizontal" >
  6. <TextView
  7. android:background="#ff0000"
  8. android:layout_width="* *"
  9. android:layout_height="wrap_content"
  10. android:text="1"
  11. android:textcolor="@android:color/white"
  12. android:layout_weight="1"/>
  13. <TextView
  14. android:background="#cccccc"
  15. android:layout_width="* *"
  16. android:layout_height="wrap_content"
  17. android:text="2"
  18. android:textcolor="@android:color/black"
  19. android:layout_weight="2" />
  20. <TextView
  21. android:background="#ddaacc"
  22. android:layout_width="* *"
  23. android:layout_height="wrap_content"
  24. android:text="3"
  25. android:textcolor="@android:color/black"
  26. android:layout_weight="3" />
  27. </linearlayout>

When all three text boxes are layout_width= "wrap_content " , you get the following effect

According to the above understanding, the system first assigns to 3 TextView their width value wrap_content (width enough to contain their content), and then the remaining screen space according to the 1:2:3 column assigned to 3 TextView, so the above image appears.

And when layout_width="fill_parent" , if the three TextView set their Layout_weight 1, 2, 2, it will appear the following effect:

You will find that the weight of 1 is small, but more points, this is why??? Many people on the internet said that when layout_width="fill_parent" , the smaller the value of weighth, the higher the priority, as if the back of the formula

, in fact, they do not really understand the problem, the real reason is layout_width= "fill_parent" cause. According to the above understanding we analyze:

The system assigns 3 TextView to their desired width fill_parent, which means that each is filled with his parent control, where the width of the dead screen

Then the remaining space at this time = 1 parent_width-3 parent_width=-2 parent_width (Parent_width refers to the screen width)

Then the actual occupied width of the first textview should be =fill_parent width, i.e. parent_width + the weight of his remaining space than column 1/5 * remaining space size ( -2 parent_width) =3/5parent_width

In the same vein the actual occupied width of the second TextView =parent_width + 2/5* ( -2parent_width) =1/5parent_width;

The actual occupied width of the third TextView =parent_width + 2/5* ( -2parent_width) =1/5parent_width; so 3:1:1 Billy shows.

Then you will understand why when you set three Layout_weight to 1, 2, and 3, the following effect appears:

The third one directly does not show, why? Let's take a look at the above method to calculate it:

The system assigns 3 TextView to their desired width fill_parent, which means that each is filled with his parent control, where the width of the dead screen

Then the remaining space at this time = 1 parent_width-3 parent_width=-2 parent_width (Parent_width refers to the screen width)

Then the actual occupied width of the first textview should be =fill_parent width, i.e. parent_width + the weight of his remaining space than column 1/6 * remaining space size ( -2 parent_width) =2/3parent_width

In the same vein the actual occupied width of the second TextView =parent_width + 2/6* ( -2parent_width) =1/3parent_width;

The actual occupied width of the third TextView =parent_width + 3/6* ( -2parent_width) =0parent_width; so 2:1:0 Billy shows. There's no room for the third one.

Android:layout_weight's deep understanding

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.