Android Development Tip One: in-depth understanding of layout_weight properties in Android layouts

Source: Internet
Author: User

Today we are going to introduce some of the techniques that are going to be used in the Android development process, all of which come from web-based collections, or when you're working on a project in your business, and using these techniques will bring convenience to our development.

first of all, to record the technical directory of this period of time, convenient for everyone to check later ( There's probably not a eve kind of tip. , about two tips per week, I will write as much detail as possible, as well as provide examples of source code):


Android Development Tip One: in-depth understanding of layout_weight properties in Android layouts

Android Development Tips II: Custom Logging Tool Classes

Android Development Skills III: Activation mode of activity

Android Development Tips Four: share a toolkit to quickly switch between Android UI threads and a toolkit for other background threads


The first is to introduce the properties of Layout_weight in the Android layout, which is a property used to allocate the remaining space, through which you can set the weight of the layout. Speaking of the rest of the space, presumably a lot of people are not special understand, I would like to introduce the rest of the space why the sacred:

First look at a piece of code:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "&G    T <edittext android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:gravity= "Left" android:background= "#22ffff" android:text= "one"/> <edittext android:layout_width= "fi        Ll_parent "android:layout_height=" wrap_content "android:layout_weight=" 1.0 "android:gravity=" center " Android:background= "#ff22ff" android:text= "/> <edittext android:layout_width=" Fill_pare        NT "android:layout_height=" Wrap_content "android:gravity=" right "android:background=" #ffff22 " android:text= "three"/></linearlayout>
Running results such as:


Let's analyze the code above: only Button2 uses the Layout_weight property and assigns it to 1, and Button1 and Button3 do not set the Layout_weight attribute, according to the API, they are assigned to 0 by default. now I'm going to say, layout_weight the real meaning of this property: The Android system is based on the 3 button height layout_height values you set wrap_content, assigning you the height of their 3, then the rest of the screen space will be assigned to Button2, because only his weight value is 1, which is why Button2 accounted for such a large piece of space.

Next, analyze This code:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" wrap_content "android:orientation=" ho Rizontal "> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_cont Ent "android:text=" 1 "android:layout_weight=" 1 "android:background=" #00ffff "/> &lt ; TextView android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:te xt= "2" android:layout_weight= "2" android:background= "#ff00ff"/> <textview Andro Id:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "3" Android : layout_weight= "3" android:background= "#ffff00"/> </LinearLayout>

As follows:


Let's focus on this piece:in the above code, we willof three text boxes  layout_width all set to wrap_content ., which means that the three pieces of content, respectively, of their own content, plus (dividing the remaining space into 1:2:3 proportions), is the size of the display space.

and whenlayout_width= "fill_parent"If you set their layout_weight to three TextView, respectively.to be1,2,2, the following effect will appear:


This time we will find that the smaller the weight, the more space allocated, then the reason is layout_width=" fill_parent " caused by the following reasons

according to the above understanding we analyze:The system assigns 3 TextView to their desired width fill_parent, which means that each is filled with his parent control, where the width of the dead screen then the remaining space at this time = 1 parent_width-3 parent_width=-2 parent_width (Parent_width refers to the screen width) then the actual occupied width of the first textview should be =fill_parent width, i.e. parent_width + the weight of his remaining space than column 1/5 * remaining space size ( -2 parent_width) =3/5parent_width The second TextView , in a similar veinthe actual occupied width =parent_width + 2/5* ( -2parent_width) =1/5parent_width;the actual occupied width of the third TextView =parent_width + 2/5* ( -2parent_width) =1/5parent_width; so 3:1:1 Billy shows.

Then you will understand why when you set three Layout_weight to 1, 2, and 3, the following effect appears:


The third one directly does not show, why? Let's take a look at the above method to calculate it:

The system first assigns 3 TextView the width fill_parent they want, which means that each one fills his parent control, and here the width of the dead screen so this time the remaining space = 1 parent_width-3 parent_width=-2 a parent_width (parent_ Width refers to screen width  ) so the actual occupied width of the first textview should be =fill_parent width, That is Parent_width + the weight of the remaining space he occupies is greater than column 1/6 * remaining space size ( -2 parent_width) =2/3parent_width Similarly the actual occupied width of the second TextView =parent_width + 2/6* ( -2parent_width) =1/3parent_width; The actual occupied width of the third TextView =parent_width + 3/6* ( -2parent_width) =0parent_width ; So it's 2:1:0 's Billy Show. The third one has no space.

This is some layout settings, there is no need to provide the source code, some of the following tips if necessary, I will free of charge to share the points to everyone, thank you!







Android Development Tip One: in-depth understanding of layout_weight properties in Android layouts

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.