LinearLayout is used very much in the Androidui layout, which has a handy and interesting property weight
, this attribute is not so simple to understand, and it is very simple to understand and feel!
This is illustrated by an example below.
Weight the meaning of the representative---Android:layout_width
The Layout code is:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Horizontal" >
<button
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "Button1"/>
<button
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:layout_weight= "5"
android:text= "Button2"/>
</LinearLayout>
Analysis:
When android:layout_width= "match_parent" , if the weight attribute is set, it takes up space according to its weight value (which can be understood as priority ), and The smaller the value, the greater the space, so it can be understood as a priority at this point.
For example: Button 1 and button 2 of the Width property is match_parent, if the button 1 of the weight= 1 button 2 is weight = 2 Then the priority button 1 occupies first, the button 2 after occupy. Size ratio is
Button 1 = 2/(1+2), button 2 = 1/(1+2) as shown in the first image below
If the button 1 weight we set to 1000, the button 2 weight is set to 1 then the button 2 almost all occupy all the space! As in the second piece
weight The meaning of the representative---android:layout_width
-
Note that the meaning of!weight will change at the fundamental line!
Look at the code first
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Horizontal" >
<button
Android:layout_width= "wrap_contenT"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "Button1"/>
<button
Android:layout_width= "wrap_content"
android:layout_height= "Wrap_content"
android:layout_weight= "1000"
android:text= "Button2"/>
</LinearLayout>
When android:layout_width= "Wrap_content", we found that even if we set the button 2 to: android:layout_weight= "1000" button 2 not just disappear as in the first case, Instead of occupying more space, what is going on here?
Analysis: The main change is that we set the Android:layout_width= "Wrap_content", which means that a control will only wrap its contents in width. If the weight is set, the meaning tells the control to wrap the content as much as possible according to weight, and the smaller the weight value , the smaller the parcel. The larger the value, the larger the package. However, the control must be able to wrap the content. Therefore, it will not disappear as the first case!
Use the Weight property correctly!
- &NBSP;
in order to improve the layout efficiency when you specify the Weight, you should the width of theedittext to is zero (0DP). Setting the width to zero improves layout performance because using "Wrap_content" as the width requires the system to Calculate a width that's ultimately irrelevant because the weight value requires another width calculation to fill the R Emaining space.
its general meaning is that if we use weight in a certain direction, we must set the width to 0DP . It tells us that the setting is 0DP because using weight, the system uses another set of algorithms to calculate the space-occupying size. (The weight value requires another width calculation to fill the remaining space.)
&NBSP;
- 2
Summary: To use weight correctly, do not go tangled
Android:layout_width= "Match_parent"
Android:layout_width= "Wrap_content" two cases how to set the weight. Because this setting is simply the wrong use.
The correct usage is:
Set android:layout_width= "0DP" or android:layout_height= "0DP" first
And then we'll get the weights.
and at this time weight also very good understanding: weight is the proportion ! Please look
Android--android Weight properties (Baidu)