Android Layout weights issues (recent layouts often pit daddy)

Source: Internet
Author: User

Android Layout Weights

With Layout_weight You can specify a size ratio between multiple views. e.g. you has a mapview and a table which should show some additional information to the map. The map should use 3/4 of the screens and table should use. Then you'll set the layout_weight of the map to 3 and the layout_weight of the table to 1.

To get it also has to set the height or width (depending on your orientation) to 0px.

Weight and parent container orientation about

Horizontal refers to the horizontal direction weight android:layout_width

Vertical refers to the vertical direction weight android:layout_height

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 control's properties Android:layout_width= "Fill_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" >
<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 the layout_weight=1,buttong2 of the layout_weight=2, the operation effect 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 stands for your control to 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" >
<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, Button1 's weight is set to 1,button2 's weight set to 2, but the effect is diametrically opposed to fill_parent. 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 design the program again, 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.

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.