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 = 0dpLook 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_parentLook at the picture:You can see the exact opposite of scenario 1 😱,tv2Instead, it occupies more proportion, so let’s calculate πŸ™‡β™€οΈ :LinearLayoutFree 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 resultstv1The final width is 1/3 of the screen width πŸ˜„.

Scene three πŸ‘‡

whenLinearLayout δΈΊ horizontal, there are two in itTextView width = wrap_contentLook at the picture:With the experience of the first two, scenario three is not difficult to draw a conclusion, let’s calculate πŸ– : due totv1The width is set towrap_contentSystem toTextViewAssign the width value, and the remaining screen width is divided in a 2:1 ratiotv1,tv2And then the calculation is the same as in scenario one.

Complimentary 🎈

mentionweightSumThis property, when we want a control to take up 1/2 or 1/3 of the screen, can be written like this:At this timetv1The 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 ~