[Android] Layout of layout_weight

Source: Internet
Author: User

On the Internet to read some of the explanation of Layout_weight, some of the more one-sided, only listed a situation, and then their own through the experiment and some relatively good article summed up, hereby recorded, for later use. Layout_weight is a linear layout, which is used in LinearLayout, the following experiment to see the characteristics of this layout_weight.

1. When the properties of the control android:layout_width= "fill_parent" or "match_parent", the layout file is as follows:

XML code
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" horizontal "android:layout_width=" Fill_parent "android:layout_height=" Fill_parent "&     Gt <button android:layout_width= "fill_parent"Android:layout_height=" Wrap_content "android:layout_weight="1"Android:text="Button1"/> <button android:layout_width="fill_parent"Android:layout_height=" Wrap_content "android:layout_weight="2"Android:text="Button2"/> </LinearLayout>

Here Button1 's layout_weight=1, Buttong2 's layout_weight=2, Layout_weight represents Important , the effect of the operation is:

We see that Button1 accounted for the 2/3,button2 accounted for 1/3. If at this time the button2 weight set to 2000, not to say Button2 disappeared, but the width of the Button1 is almost full of screen width, and the last trace of the screen is left to Button2, is nearly very small, not shown. As follows:

Concluded that: when Layout_width is set to Fill_parent, Layout_weight represents that your control should take precedence as large as possible, but as large as possible is limited, that is, fill_parent.

2. When the control's properties Android:layout_width= "wrap_content", the layout file is as follows:

XML code
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" horizontal "android:layout_width=" Fill_parent "android:layout_height=" Fill_parent "&     Gt <button android:layout_width= "wrap_content"Android:layout_height=" Wrap_content "android:layout_weight="1"Android:text="Button1"/> <button android:layout_width="wrap_content"Android:layout_height=" Wrap_content "android:layout_weight="2"Android:text="Button2"/> </LinearLayout>

Similarly, the weight of Button1 is set to 1, the weight of Button2 is set to 2, the layout_weight represents the scale , But the effect is diametrically opposite to the fill_parent effect. The results are as follows:

At this point, and fill_parent exactly the opposite, the width of the Button1 occupy the screen width of 1/3, and the width of the Button2 occupy the screen 2/3, if the Button1 weight set to 2000, as previously understood, Button1 should be small, almost invisible on the screen, but wrong, the experiment tells us, when Button1 weight very hour, also want to "wrap_content", that is to ensure that Button1 at least can display. The following is the run when the Button1 setting weight to 2000:

We see that the Button1 is small enough, but to ensure that he can show it, and therefore concludes:

When Layout_width is set to Wrap_content, Layout_weight stands for your control to take precedence as small as possible, but this small is limited, that is, wrap_content.

When we re-design the program, we can use this weight property to divide the screen height or width by its own proportions in order to be able to adapt to the screen and not give the control a specified width and height.

Related Article

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.