Android Layout-layout_weight

Source: Internet
Author: User

This talk about the weight of the layout, nonsense not much to say, straight to the subject.

First we create a lineatlayout layout and set it to landscape, placing three text boxes in the layout tv1;tv2;tv3

1, when we give three TextView width are set to Layout_width= "Fill_parent", set three text box weights are 1:2:2; At this point three text boxes appear in the layout: TV1, TV2, TV3 are displayed, And the scale of three text boxes in the layout is tv1:tv2:tv3=3:1:1.

Analysis: The system first assigns 3 text boxes the width they need (fill_parent), according to the formula: actual width = set width + weight ratio * remaining space. Then the remaining space = 1 parent_width-3 parent_width=-2 parent_width (Parent_width refers to the width of the screen), TV1 the actual width =fill_parent (parent_ width) +1/5* (-2 x parent_width) =3/5 parent_width;tv2 actual occupied width =fill_parent (parent_width) +2/5* (-2 parent_width) =1/ 5 PARENT_WIDTH;TV3 The actual occupied width =fill_parent (parent_width) +2/5* (-2 parent_width) =1/5 Parent_width, so three text boxes in the layout of the display ratio is TV1 : Tv2:tv3=3:1:1.

2, when we give three TextView width are set to Layout_width= "Fill_parent", set three text box weights are 1:2:3, the layout only shows TV1 and TV2, and the display of the text box scale is tv1:tv2=2:1.

Analysis: Remaining space = 1 parent_width-3 parent_width=-2 parent_width;tv1 actual occupied width =fill_parent (parent_width) +1/6* (-2 Parent_ width) =2/3 parent_width;tv2 actual occupied width =fill_parent (parent_width) +2/6* (-2 parent_width) =1/3 parent_width;tv3 actual occupied width =fill_parent (Parent_width) +3/6* (-2 Parent_width) = 0 Parent_width, so the text box scale shown is tv1:tv2=2:1.

3, if the width of three text boxes are set to Wrap_content or 0DP, the weight ratio tv1:tv2:tv3=1:2:3, the layout of three text boxes are displayed, and the scale is tv1:tv2:tv3=1:2:3.

Analysis: The system first assigns 3 text boxes their width value wrap_content, then assigns the remaining screen space to 3 text boxes according to the 1:2:3 proportion, therefore the layout displays the text box scale is tv1:tv2:tv3=1:2:3.

Android Layout-layout_weight

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.