Android: a deep understanding of Layout_weight,

Source: Internet
Author: User

Android: a deep understanding of Layout_weight,

I recently wrote a Demo and suddenly found the Layout_weight attribute. I found a lot of interesting discussions on this attribute on the Internet. However, I found a lot of information and did not find a clear one, so I studied it on the Internet and finally learned how to share it with you.

First, let's take a look at the role of the Layout_weight attribute: it is used to allocate an attribute belonging to a space. You can set its weight. Many people do not know what the concept of surplus space is. Let's talk about it first.

See 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="two"/>     
  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 running result is:

See the code above: Only Button2 uses the Layout_weight attribute and is assigned to 1, while Button1 and Button3 do not set the Layout_weight attribute. According to the API, they are 0 by default.

The true meaning of the Layout_weight attribute is as follows: the Android system first allocates the Layout_height value wrap_content based on the three buttons you set,

Then, all the remaining screen space will be assigned to Button2, because only the weight value is 1, which is why Button2 occupies such a large space.

With the above understanding, we can have a clear understanding of the confusing Effect of Layout_weight on the Internet.

Let's look at this 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 the three text boxes contain layout_width = "wrap_content", the following results are displayed:


According to the above understanding, the system first assigns three textviews their width values wrap_content (the width is sufficient to include their content 1, 2, 3 ), then, the remaining screen space is allocated to three textviews according to the ratio, so the above image is displayed.

When layout_width = "fill_parent", if the Layout_weight of the three textviews is set to 1, 2, and 2 respectively, the following results will be displayed:


You will find that the weight of 1 is small, but the score is much higher. Why ??? Many people on the Internet say that when layout_width = "fill_parent", the smaller the weighth value, the larger the weight, the higher the priority.

In fact, they did not really understand this problem. The real cause is caused by Layout_width = "fill_parent. According to the above understanding, we will analyze:

The system first assigns three textviews the fill_parent width they want, that is to say, each of them is filled with its parent control. Here, the screen width is dead.

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

The actual width of the first TextView should be equal to the width of fill_parent, that is, parent_width + the weight of the remaining space occupied by parent_width = 3/5parent_width

Similarly, the actual width occupied by the second TextView is parent_width + 2/5 * (-2parent_width) = 1/5parent_width;

The actual occupied width of the third TextView is parent_width + 2/5 * (-2parent_width) = 1/5parent_width; therefore, the ratio column at is displayed.

In this way, you will understand why the following effects will occur when you set the three Layout_weight to 1, 2, and 3:

The third is not displayed. Why? Let's take a look at the above method:

The system first assigns three textviews the fill_parent width they want, that is to say, each of them is filled with its parent control. Here, the screen width is dead.

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

The actual width of the first TextView should be equal to the width of fill_parent, that is, parent_width + the weight of the remaining space occupied by parent_width = 2/3parent_width

Similarly, the actual width occupied by the second TextView is parent_width + 2/6 * (-2parent_width) = 1/3parent_width;

The actual width of the third TextView = parent_width + 3/6 * (-2parent_width) = 0parent_width; therefore, it is displayed in the ratio column. The third option has no space.


The sub-element attribute android: layout_weight in LinearLayout has other features.

I don't know what you are asking. I currently use this property. For example, one row has two controls, and each control
Android: layout_weight = "1" and they will divide the region equally.
For example, if you have a TextView, the following ListView only needs to set android: layout_weight = "1" for the listview to make the textview not covered during the listview drop-down.

Why can the android layout_weight attribute display controls in a certain proportion?

Layout_weight is the weight attribute of the component in the layout. A larger value indicates more parts of the site.

Related Article

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.