Often develop the small partner of the Android this attribute certainly can’t be familiar with again, but its calculation principle perhaps few people know, next, look at π~
Scene one π
whenLinearLayout
δΈΊ horizontal
, there are two in itTextView
width = 0dp
Look at the picture. I’ve highlighted the important information in red:As you can see, the controls are displayed according to the specific weight we set, so their actual calculation process looks like this:
The thing to understand here is that layout_weight is for the rest of the space.
Tv1 (width) = tV1’s original width + 2/(2+1) * (remaining space) = 0 + 2/3 * W(screen width) = 2/3 screen width; So why is the remaining width of the LinearLayout the width of the screen? Because the remaining width is equal to W minus 0(width tv1) minus 0(width tv2), so it’s equal to W.
Scene two π
whenLinearLayout
δΈΊ horizontal
, there are two in itTextView
width = match_parent
Look at the picture:You can see the exact opposite of scenario 1 π±,tv2
Instead, it occupies more proportion, so let’s calculate πβοΈ :LinearLayout
Free space = W(screen width) -w (width of tv1) -w (width of tv2) = -w Tv1 (width) = W(original width of Tv1) + 2/3 * Free space = W – 2*3W = 1/3W According to the calculation resultstv1
The final width is 1/3 of the screen width π.
Scene three π
whenLinearLayout
δΈΊ horizontal
, there are two in itTextView
width = wrap_content
Look at the picture:With the experience of the first two, scenario three is not difficult to draw a conclusion, let’s calculate π : due totv1
The width is set towrap_content
System toTextView
Assign the width value, and the remaining screen width is divided in a 2:1 ratiotv1,tv2
And then the calculation is the same as in scenario one.
Complimentary π
mentionweightSum
This property, when we want a control to take up 1/2 or 1/3 of the screen, can be written like this:At this timetv1
The width of the screen takes up 1/3 of the screen. Over, if there is something wrong, welcome ππ, if it helps you, remember to click π oh ~