Rules for using Layout_weight properties in Android ui:linearlayout

Source: Internet
Author: User

First look at the Android SDK documentation, and there's a phrase

LinearLayoutalso supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of what much space is should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value, and then any remaining space in the view group are assigned to children in the prop Ortion of their declared weight. Default weight is zero.

We can see from the document that the LinearLayout neutron control can use the Layout_weight attribute to allocate the "weight", but the actual use of the process, often error, will make people feel puzzled, the following is a summary of some of the experience:

Take the horizontal (perpendicular direction) LinearLayout as an example:

<linearlayout    android:layout_width= "match_parent"    android:layout_height= "Wrap_content"    android: layout_marginleft= "16DP"    android:layout_marginright= "16DP" ><button    android:id= "@+id/crime_date"    android:layout_width= "wrap_content"    android:layout_height= "wrap_content"    android:layout_weight= "1 "/><checkbox    android:id=" @+id/crime_solved "    android:layout_width=" Wrap_content "    android: layout_height= "Wrap_content"    android:layout_weight= "1"    android:text= "@string/crime_solved_label"/ ></LinearLayout>

LinearLayout is a horizontal line layout that determines how much space should be allocated to each child member Layout_width and Layout_weight, following these rules:

    1. first The space is initially specified based on the value of Layout_width, because Layout_width are wrap_content, and the layout manager gives enough space for the two child controls to be stretched horizontally, resulting in the following image:
    2. then , because there is still enough space in the horizontal direction, the layout manager assigns the extra control to the value of layout_weight. The above example is 1:1, then the extra controls are allocated at a scale of 1:1, and the effect becomes:
      •   
    3. If you want to set two controls to the same width, Android recommends that you set the properties of the two controls so: LAYOUT_WIDTH=0DP, layout_weight=1
    4. If a control does not provide Layout_weight, then Android defaults to 0.
    5. Because different programmers have different routines for writing code, Android allows layout_weight values to be decimals or integers, and the allocation scale is the respective (layout_weight value)/(the value added by the respective layout_weight), The premise of this rule is that LinearLayout does not set android:weightsum to limit the sum.

Let's take a look at one of the often misunderstood examples:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:orientation=" horizontal "    android:layout_width=" match_parent "    android:layout_ height= "Match_parent" >    <textview        android:layout_height= "wrap_content"        android:text= "small"        android:textcolor= "#02F"        android:layout_width= "wrap_content"        android:layout_weight= "1"/>    <textview        android:layout_height= "wrap_content"        android:textcolor= "#F20"        android:text= "A very Very long text that needs to wrap. A very very long "        android:layout_width=" wrap_content "        android:layout_weight=" 1 "/></linearlayout >

The effect is this:

It does not follow the weight to 1:1 of the allocated space ratio, because the layout manager satisfies the layout_width= ' wrap_content ' according to the first rule, in which case, if you really want to allocate by 1:1 of the space, you can only set the Layout_ Width= ' 0DP ', this is how it works:

Welcome Treatise

Rules for using Layout_weight properties in Android ui:linearlayout

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.