Wonderful properties: layout_weight explanation and usage

Source: Internet
Author: User

In the control layout of Android, there is a wonderfulLayout_weightAttribute, defined as follows:

Layout_weight:Used to specify the remainderIdleSpace. Usage:

01 <LinearLayout
02   android:orientation="horizontal">
03  
04   <TextView
05       android:layout_width="wrap_content"
06       android:layout_height="wrap_height"
07       android:layout_weight="1"
08       android:text="888"/>
09  
10   <TextView
11       android:layout_width="wrap_content"
12       android:layout_height="wrap_height"
13       android:layout_weight="1"
14       android:text="999999"/>
15  
16 </LinearLayout>

Why is it amazing?

The preceding layout code is used as an example,TextView-888AndTextView-999999Is two controls horizontally arranged.Layout_weight = "1", Indicating the two controlsThe remaining free space of the LinearLayoutWe can easily mistakenly think that the two controls divide the horizontal space, that is, each occupies 50% of the width.

This is actually wrong.,:The width occupied by the TextView-999999 ControlDegrees>Width occupied by TextView-888. Because the width occupied by 999999 characters is greater than the width occupied by 888 characters, that is:W (999999) + 1/2 free space> w (888) + 1/2 free space.

This is a wonderful place. It is easy for us to mistakenly think that it is the entire control space. At this point, we will surely think that, in this case, the layout_weight attribute makes no sense. I thought it could allocate space, but it only split the remaining free space.

In fact, layout_weight can be used to split the entire space. If we define the width of the control as 0, in this way, for example, the layout_weight = "1" of the two controls can divide the entire space by 50%, because:0 + 1/2 Free Space = 0 + 1/2 Free Space

This is a trick and a practical layout_weight splitting solution: defining controlsLayout_width = "0dp"OrLayout_height = "0dp"With layout_weight, you can divide the entire space.

The layout_width = "0dp" and layout_weight = "1" of the two controls are defined below, achieving an average segmentation of 50% in the horizontal direction:

01 <LinearLayout
02   android:orientation="horizontal">
03  
04   <TextView
05       android:layout_width="0dp"
06       android:layout_height="wrap_height"
07       android:layout_weight="1"
08       android:text="888"/>
09  
10   <TextView
11       android:layout_width="0dp"
12       android:layout_height="wrap_height"
13       android:layout_weight="1"
14       android:text="999999"/>
15  
16 </LinearLayout>

The following defines the layout_height = "0dp" and layout_weight = "1" of the two controls to achieve an average split of 50% in the vertical direction:

01 <LinearLayout
02   android:orientation="vertical">
03  
04   <TextView
05       android:layout_width="wrap_content"
06       android:layout_height="0dp"
07       android:layout_weight="1"
08       android:text="888"/>
09  
10   <TextView
11       android:layout_width="wrap_content"
12       android:layout_height="0dp"
13       android:layout_weight="1"
14       android:text="999999"/>
15  
16 </LinearLayout>

Layout_weight can be used in this way.

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.