Detailed explanation of weight (weight) attribute:
①The simplest usage:
As shown in the picture:
Implementation code:
<?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="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="0dp" android:background="#ADFF2F" android:text="weight is 1" android:layout_weight="1"/> <TextView android:layout_width="fill_parent" android:layout_height="0dp" android:background="#DA70D6" android:text="weight is 2" android:layout_weight="2"/>
To achieve the first 1:1 effect, you only need to put the weight of the two LinearLayouts separately Change it to 1 and 1. Usage summary: Divide the vertical direction according to the proportion: set the android:width attribute of the involved View to 0dp, and then set it to the android weight attribute to set the ratio; by analogy, for the horizontal direction, just Set android:height to 0dp, and then set the weight attribute! You can write a horizontal division of equal proportions to experience the simple usage!
②Detailed explanation of weight attribute:
Of course, if we do not apply the above-mentioned method of setting 0dp, use wrap_content and match_parent directly , it is necessary to analyze the weight attribute, which is divided into two cases, wrap_content and match_parent! In addition, it depends on whether the orientation of LinearLayout is horizontal or vertical, which determines which direction is divided in equal proportions
1)wrap_content It’s relatively simple, it’s directly proportional
Implementation code:
<?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="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ADFF2F" android:text="weight is 1" android:layout_weight="1"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#DA70D6" android:text="weight is 2" android:layout_weight="2"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#00ff66" android:text="weight is 3" android:layout_weight="3"/>
2)match_parent (fill_parent): This needs to be calculated
We write this simple code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_heightfill_parent(match_parent),But there is only one screen,Then1 - 3 = - 2 fill_parentstep 2: The ratio is 1/6,2/6,3/6 span>
step 3: first come, first served,First giveone,calculate: 1 - 2 * (1/6) = 2/3 fill_parent Continue totwo,Calculation: 1 - 2 * (2 /6) = 1/3 fill_parent finallythree,calculate1 - 2 * (3/6) = 0 fill_parent
step 4: So the final result is:onetakes two, twotakes one,threeThere is nothing. The above is whythreeThere is no reason for it,Maybe everyone is still a bit confused after reading it,It’s okay, Let’s give a few more examples and try it out!
The ratio is: 1:1:1
According to the above calculation method, the result is: 1/3 1/3 1/3, Yes
Then we will Try: 2:3:4
Calculation result: 5/9 3/9 1/9, comparison effect diagram, 5:3: 1, that's right, so you can mark this calculation method!
③Set the weight attribute in the Java code:
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));0)">(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, 1));