Android:layout_width and Android:width in Android

Source: Internet
Author: User

Then I'm going to recap this sentence: "Because the weight of the button1 is the smallest, so it occupies a higher priority of the layout," perhaps in the Android layout and there is no priority to say, I here just to illustrate the problem, I define, so friends do not shoot bricks.
Let's start with the analysis. When the Layout_widtd property is set to Fill_parent, the parent layout is full, which means that the control is as large as possible under the weight setting, so, in the example above, the weight of Button1 is set to 1, The weight of the button2 is set to 2. That is, the button has the highest priority, so it is necessary to populate the parent layout button1 First, as large as possible, and this is as much as possible, which is to synthesize the weight value of the other controls in layout. 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, which can be seen in graphical layout mode when the button1 fills the entire width, And do not see the shadow of Button2, in fact, button2 still exist, you put the mouse pointer to the back of the button1 can see a long vertical bar, that is button2, is very very small.

Therefore, when Layout_width is set to Fill_parent, the smaller the weight weight represents, the greater the priority of your control as possible.


     then when Layout_weight is set to wrap_content, it adapts to the width of the content, This means that the control should be as small as possible, as long as the content can be displayed, the same, if the Button1 and Button2 Layout_weight set to Wrap_content, 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, just as the first grade is different, because the setting of the weight, so the total width of the two controls to fill the width of the parent, so you have to calculate each control occupy the size, at this time, button1 of the first grade higher, a total of two, a 1/3, a copy of the 2/3,button1 to be as small as possible, that button1 of course to choose 1/3, so we see the effect is button2 occupation is larger. The thing to say here is that if you do the same thing, set the following: Button1 for 1,button2 2000, is the Button1 going to occupy 1/2000 of the space? So the understanding is wrong, just said, to be as small as possible, but this small one has a limit, that is wrap_content, It is also that if there is a full display of the content, the same, as possible, there is a limit, that is the width of the father's office. So, when Layout_width is set to Wrap_content, weight represents your control to be as big as possible.

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 to be as big as possible, but this is a big limit, that is, fill_parent.
When Layout_width is set to Wrap_content, Layout_weight represents your control to be as small as possible, but this small is limited, that is, wrap_content.

Layout_height with Layout_width.

(traditional characters are not looking very upset, haha)

Here are a few pictures to illustrate:

1. Layout_width= "Fill_parent", weight=2 of Button1 Weight=1,button2;


2.layout_width= "Fill_parent", the weight=2000 of Button1 Weight=1,button2;

--------------------------------------------------------------------------------------------------------------- ---------------------------------

3.layout_width= "Wrap_content", the weight=2 of Button1 Weight=1,button2;

4.layout_width= "Wrap_content", the weight=2000 of Button1 Weight=1,button2;

Android:layout_width and Android:width in Android

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.