"Go" Android layout: Layout_weight's understanding

Source: Internet
Author: User

Android:layout_weight Detailed Analysis Introduction:

The layout file is:
<?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>


The layout that appears is: Button1 accounted for the 2/3,button2 accounted for 1/3.

However, if you change the property of the button in the layout file android:layout_width= "fill_parent" to Android:layout_width= "wrap_content" The result is: Button1 accounted for 1 /3,button2 accounted for 2/3.

What does it mean to end up like this? Here is a detailed explanation:

When a child with weight is included in the LinearLayout, LinearLayout will measure two times:
Set the screen width to X
First: Button1 's measuredwidth is X,button2 and X (because weight is used, so linearlayout does not take into account the size of the previous one each time measure child), Total_width is 2X
Second: Calculates the delta=x-total_width=-x and then sets the width of the button1 to
x+delta*1/3=0.66x, the width of the button2 is x+delta*2/3=0.33x


So I'm going to recap this sentence: "Because the button1 is set to the smallest weight, it occupies a higher layout priority", that is, the smaller the layout_weight, the higher the layout priority. Perhaps there is no priority in the layout of Android, I am here just to illustrate the problem, I define, so friends do not shoot bricks.
Well, first of all, when the Layout_width property is set to Fill_parent, the parent layout is full, which means that the control should be as large as possible according to the weight setting, so the weight of Button1 is set to 1, according to the above example. The weight of the button2 is set to 2. That is, the Button1 is the highest priority, so to populate the parent layout is button1 first to fill, as large as possible, and this is as much as possible, this will be integrated layout of the other controls weight value, and then do the operation, Button1 occupy 2 /3,button2 occupy 1/3. You can also set the button2 to a very large number, such as 2000, at this time in the graphical layout mode can see the button1 filled full width, and not see the shadow of Button2, In fact button2 still exist, you point the mouse to button1 behind can see a long vertical bar, that is button2, is very very small. Therefore, when Layout_width is set to Fill_parent, weight represents that your control should take precedence as large as possible.

Then when the layout_weight is set to Wrap_content, that is, to adapt to the width of the content, meaning that the control to be as small as possible, as long as the content can be displayed on it, the same, if the Button1 and Button2 Layout_ After the weight is set to Wrap_content, the button1 weight is 1, Button2 's weight is 2. So button1 to be as small as possible, and button2 to be as small as possible, but with a different priority, because weight is set, so the total width of the two controls fills the width of the parent layout, so it's time to calculate the size of each control, and then button1 Higher priority, a total of two, a 1/3, a copy of 2/3,button1 to as small as possible, that button1 of course to choose 1/3, therefore, we see the effect is button2 occupy the larger. Here is to say that if the weight is the same as the following settings: Button1 for 1,button2 2000, that Button1 is to occupy 1/2000 of the space it? So understand is wrong, just said, to be as small as possible, but this small there is a limit, that is wrap_content, is also if the content finished full display, the same, as large as possible there is a limit, that is the width of the parent layout. So, when Layout_width is set to Wrap_content, weight represents your control as small as it takes precedence.


So, to understand the weight, we must deeply understand the following two words:
When Layout_width is set to Fill_parent, Layout_weight represents your control as large as possible, but this is limited, that is, fill_parent.
When Layout_width is set to Wrap_content, Layout_weight represents that your control should take precedence as small as possible, but this small is limited, that is, wrap_content.
Layout_height with Layout_width.

In fact, the above explanation is mainly for fill_parent and wrap_content interpretation, we can refer to the following 2 blog posts to know that the above explanation is a certain problem.

In general, we use layout_weight, we may use in various controls or layouts (like LinearLayout), the most widely used is the layout of the Dv6300-t main interface, all with the layout_weight to control, This allows for better adaptation to different resolutions.

So we often use layout_weight to set the layout, usually set different linearlayout for the different layout_weight to complete the basic layout, but also can use the LinearLayout to occupy the space is not any display, This is the full realization of the proportion of the layout of the LinearLayout, and finally in each linearlayout to implement a plurality of control layout_weight control, you can also use only the control is not displayed control to achieve the effect of distribution layout, Dv6300-t is a clear example.

Also need to note is LinearLayout orientation if not set, then the default is horizontal horizontal direction.

To display the linearlayout in a proportional format, set the Android:layout_width= "0DP"

If the setting is Android:layout_height= "0DP" in the vertical direction.

In this case, the proportion of a child control occupying LinearLayout is the weight of the value/weight value of all controls within the linearlayout of this control.

In general, we control the proportion is to set the corresponding direction of high or wide 0dp to achieve the specific gravity we want to control, and rarely use fill_parent or wrap_content, but we understand the differences between the 2 settings.

One more thing to understand is that the weight (layout_weight) is the smallest, so it occupies a higher layout priority.

It is also important to note that the consistency control is preserved :

If we want to control the display specific gravity, then we generally require the elements of the various levels of control must be consistent, such as multiple button control at the same level to control the proportion of the layout, then if the set is Android:layout_width= "0DP" way to control, All level controls must be controlled using android:layout_width= "0DP", but not with android:layout_width= "0DP" and some with android:layout_width= "Fill_ Parent ", so there will be confusion.

Here's another example:

<?xml version= "1.0" encoding= "Utf-8"?>

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"

android:orientation= "Vertical"

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent" >

<linearlayout

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent"

android:layout_weight= "1" >

<button

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent"

Android:layout_weight= "2"

Android:background= "#00AAC0"

/>

<button

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent"

Android:layout_weight= "2"

Android:background= "#CCdd00"

/>

<button

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent"

android:layout_weight= "1"

Android:background= "#0000FF"

/>

</LinearLayout>

<linearlayout

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent"

android:layout_weight= "2" >

<button

Android:layout_width= "Wrap_content"

android:layout_height= "Fill_parent"

Android:layout_weight= "2"

Android:background= "#FFAA00"

/>

<button

Android:layout_width= "Wrap_content"

android:layout_height= "Fill_parent"

Android:layout_weight= "2"

Android:background= "#CCdd00"

/>

<button

Android:layout_width= "Wrap_content"

android:layout_height= "Fill_parent"

android:layout_weight= "1"

Android:background= "#0000FF"

/>

</LinearLayout>

<linearlayout

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent"

android:layout_weight= "2" >

<button

Android:layout_width= "0DP"

android:layout_height= "Fill_parent"

Android:layout_weight= "2"

Android:background= "#00FF00"

/>

<button

Android:layout_width= "0DP"

android:layout_height= "Fill_parent"

Android:layout_weight= "2"

Android:background= "#CCdd00"

/>

<button

Android:layout_width= "0DP"

android:layout_height= "Fill_parent"

android:layout_weight= "1"

Android:background= "#cc00FF"

/>

</LinearLayout>

</LinearLayout>

Look at the final:

"Go" Android layout: Layout_weight's understanding

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.