Objective
Layout_weight This property in Android is certainly not strange to us, which is often a UI-crunching. However, when we really use this property, there are often some strange and wonderful layout effect, if we just know it but do not know why, some unexpected layout effect must make us quite a headache. In this article, the Layout_weight attribute is analyzed in detail. Body Starting from code: <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <Button android:layout_width= " Match_parent " android:layout_height=" wrap_content " android:text= "Button1" /> <Button Android:layout_width = "Match_parent" android:layout_height= "wrap_content" Android: text= "Button2" /> <Button Android:layout_ Width= "Match_parent" &NBSp android:layout_height= "Wrap_content" android:text= "Button3" / ></LinearLayout> we can see that these three buttons do not occupy the entire screen, but are based on the size of the content. We add an attribute android:layout_weight= "1" to the button2, and we find the layout to be like this: we found that the screen was occupied by three buttons, And the first and third buttons remain the same size as the content, while the second button takes up the remaining screen space. We explain the above: 1, we only use the Layout_weight property in Button2, and assign a value of 1, but Button1 and button2 do not use this property, according to the API can know that their layout The _weight property equals 0. &NBSP;2, LinearLayout If the Layout_weight attribute is explicitly included, it is measure two times, the width of the three button is calculated normally for the first time, and the remaining space is allocated for the second time with the value of Layout_weight. Popular point to summarize: the Android system first according to your set of 3 button height Layout_height=wrap_content, assign you the height of their 3, and then the remaining screen space will be assigned to Button2, Because only his weight value is 1, which is why Button2 occupies such a large piece of space. So, we probably have a clearer understanding of the Layout_weight attribute. Take a look at the following code: <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" Android:o Rientation= "Horizontal" > <TextView android:layout_width= "wrap_content " android:layout_height=" wrap_content " android:layout_weight=" 1 " android:text= "1" android:background= "#ff0000" /> <TextView android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_weight= "2" & nbsp android:text= "2" android:background= "#00ff00" />& nbsp <TextView android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layout_weight= "3" android:text= "3" AndroidOid:background= "#0000ff" /></linearlayout> in our layout_ on these three TextView When width is set to wrap_content, we get the following layout: this layout fits well with our analysis above: system assigns their width value to 3 TextView first WRAP_ Content (which is wide enough to contain their contents) and then assigns the remaining screen space to 3 TextView according to the 1:2:3 column, so the image above appears. If we set the above three TextView layout_width to Match_parent, and set their layout_weight to 1, 2, 2 for each of the three TextView, there will be a reverse layout effect: we can probably tell that the percentage they occupy is 3:1:1. This will result in one more effect: the larger the weight weight, the smaller the space the control takes up. So there's someone on the internet that summarizes: when Layout_width is set to Match_parent, Layout_weight represents your control as large as possible, but this big is limited, that is, match_parent. When Layout_width is set to Wrap_content, Layout_weight represents that your control should take precedence as small as possible, but this large is limited, that is, wrap_content. This is the practice of just remembering how to use it and not why it is, as a programmer who has the desire to have the ideal, will not agree, because maybe one day, we forget the formula, so we have to understand the principle of why this is the case. according to the above understanding, the appearance of this phenomenon lies in layout_width= "fill_parent" this cause; start the following analysis: 1, The system assigns 3 TextView to their desired width match_parent, and specifies that each textview is filled with his parent control, which is the width of the entire screen. &NBSP;2, because if contains layout_width this attribute, LinearLayout will carry on the second time measure, at this time will allocate the remaining space according to the weight, here remaining space = 1 parEnt_width-3 a parent_width=-2 parent_width (Parent_width refers to the screen width). 3, the actual occupied width of the first TextView =parent_width (width of match_parent) + 1/5 (the weight of his remaining space Billy) * remaining space size ( -2 parent_width) =3/5parent_ width 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 we can see why this ratio is shown by 3:1:1. What if we layout_weight 1, 2, 3? The third textview disappeared straight away with wood there! we can follow the above method to arrive at the answer: 1, the system first assigned to 3 textview their desired width match_parent, also specified that each textview is to fill his parent control, This is the width of the entire screen. &NBSP;2, because if contains layout_width this attribute, LinearLayout will carry on the second time measure, at this time will allocate the remaining space according to the weight, here the remaining space = 1 parent_width-3 parent_ Width=-2 a parent_width (Parent_width refers to the width of the screen). 3, the actual occupied width of the first TextView =parent_width (width of match_parent) + 1/6 (the weight of his remaining space Billy) * remaining space size ( -2 parent_width) =2/3parent_ width 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 three textview of the explicitThe scale should be 2:1:0, and the third TextView will disappear, because there is no room for him to be allocated according to the calculations. See here we all know this calculation principle, I believe that for the design of UI layout is helpful, for match_parent and wrap_content mixed use of the UI layout can also be calculated by this method of design. then we'll take it with you. Another property associated with Layout_weight:weightsum as the name implies, is the total value of the weight, if not specified WeightSum, the system will be each layout The sum of the values of the _weight is summed as the total value of the weight, of course, we can also be explicitly specified here, it is also convenient for us to use the layout, I would like to give an application about the Weightsum property: see such an effect, A button that always takes up half the screen actually the XML code is simple: <linearlayout xmlns:android= "/http Schemas.android.com/apk/res/android " android:layout_width=" match_parent " Android:layout_ height= "Match_parent" android:gravity= "center" android:weightsum= "2" Android : orientation= "Horizontal" > <Button android:layout_width= "0DP" android:layout_height= "wrap_content" android:layout_weight= "1" & nbsp android:text= "button" /></linearlayouThe Layout_weight attribute is described in the t> development document as follows: Defines the maximum value of the weight sum. If this value is not specified, the cumulative value of the Layout_weight property of all child views is used as the maximum value of the sum. A typical case is: by specifying the Layout_weight property of the child view to 0.5, and setting the LinearLayout's Weightsum property to 1.0, the implementation child view occupies 50 of the available width. from the XML above we can see that we set the Weightsum to 2, and for the button set Layout_width is 0 and Layout_weight is 1 (that is, half of weightsum), we can draw the above one effect , which keeps the button occupied by half the screen, rather than hard-coded implementations.
A companion tour, a free dating site: www.jieberu.com
Push family, free tickets, scenic spots: www.tuituizu.com
Android Knowledge Point Anatomy Series: in-depth understanding of layout_weight properties