Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of Layout_weight in Android

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what is the use of Layout_weight in Android". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the use of Layout_weight in Android"?

First of all, look at the role of the Layout_weight attribute: it is used to assign an attribute that belongs to the space, and you can set its weight. Many people do not know what the concept of remaining space is, so let me first talk about the remaining space.

Look at the following code:

The result of the operation is:

Look at the above code: only Button2 uses the Layout_weight attribute and assigns a value of 1, while Button1 and Button3 do not set the Layout_weight attribute. According to API, they default to 0.

Let me tell you what the Layout_weight property really means: the Android system first assigns you the height of the three Button heights Layout_ height value wrap _ content according to the three heights you set.

Then all the remaining screen space is assigned to Button2, because only his weight value is 1, which is why Button2 takes up such a large piece of space.

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

Let's take a look at this code:

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

According to the above understanding, the system first allocates their width value wrap_content to the three TextView (wide enough to contain their content, 1magin2 and 3), and then allocates the remaining screen space to the three textview according to the 1:2:3 ratio, so the above image appears.

When layout_width= "fill_parent", if you set their Layout_weight to 1, 2, 2 for the three TextView respectively, the following effect will occur:

You will find that the weight of 1 is small, but it is more divided. Why is this? Many people on the Internet say that when layout_width= "fill_parent", the smaller the weight value, the greater the weight, the higher the priority, which is like memorizing the formula.

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

The system first assigns the three textview the width fill_parent they want, that is to say, each of them is filled with his parent control, and here is 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 width occupied by * TextView should be equal to the width of fill_parent, that is, the weight of the remaining space occupied by parent_width + him is greater than the weight of the remaining space occupied by parent_width 5 * the size of remaining space (- 2 parent_width) = 3/5parent_width.

In the same way, the actual width of the second TextView = parent_width + 2 TextView 5 * (- 2parent_width) = 1/5parent_width

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

In this way, you can see why when you set the three Layout_weight to 1, 2, 3, the following effect occurs:

The third one is not displayed directly. Why? Let's calculate according to the above method:

The system first assigns the three textview the width fill_parent they want, that is to say, each of them is filled with his parent control, and here is 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 width occupied by * TextView should be equal to the width of fill_parent, that is, the weight of the remaining space occupied by parent_width + him is greater than that of column 1 parent_width 6 * size of remaining space (- 2 parent_width) = 2/3parent_width

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

The actual width of the third TextView = parent_width + 3 2parent_width 6 * (- 2parent_width) = 0 parentages width; so the 2:1:0 column shows. There is no room for the third one.

Thank you for your reading, the above is the content of "what is the use of Layout_weight in Android". After the study of this article, I believe you have a deeper understanding of what the use of Layout_weight in Android has, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report