In the control layout of Android, there is a wonderfulLayout_weightAttribute, defined as follows:
Layout_weight:Used to specify the remainderIdleSpace. Usage:
02 |
android:orientation="horizontal"> |
05 |
android:layout_width="wrap_content" |
06 |
android:layout_height="wrap_height" |
07 |
android:layout_weight="1" |
11 |
android:layout_width="wrap_content" |
12 |
android:layout_height="wrap_height" |
13 |
android:layout_weight="1" |
14 |
android:text="999999"/> |
Why is it amazing?
The preceding layout code is used as an example,TextView-888AndTextView-999999Is two controls horizontally arranged.Layout_weight = "1", Indicating the two controlsThe remaining free space of the LinearLayoutWe can easily mistakenly think that the two controls divide the horizontal space, that is, each occupies 50% of the width.
This is actually wrong.,:The width occupied by the TextView-999999 ControlDegrees>Width occupied by TextView-888. Because the width occupied by 999999 characters is greater than the width occupied by 888 characters, that is:W (999999) + 1/2 free space> w (888) + 1/2 free space.
This is a wonderful place. It is easy for us to mistakenly think that it is the entire control space. At this point, we will surely think that, in this case, the layout_weight attribute makes no sense. I thought it could allocate space, but it only split the remaining free space.
In fact, layout_weight can be used to split the entire space. If we define the width of the control as 0, in this way, for example, the layout_weight = "1" of the two controls can divide the entire space by 50%, because:0 + 1/2 Free Space = 0 + 1/2 Free Space
This is a trick and a practical layout_weight splitting solution: defining controlsLayout_width = "0dp"OrLayout_height = "0dp"With layout_weight, you can divide the entire space.
The layout_width = "0dp" and layout_weight = "1" of the two controls are defined below, achieving an average segmentation of 50% in the horizontal direction:
02 |
android:orientation="horizontal"> |
05 |
android:layout_width="0dp" |
06 |
android:layout_height="wrap_height" |
07 |
android:layout_weight="1" |
11 |
android:layout_width="0dp" |
12 |
android:layout_height="wrap_height" |
13 |
android:layout_weight="1" |
14 |
android:text="999999"/> |
The following defines the layout_height = "0dp" and layout_weight = "1" of the two controls to achieve an average split of 50% in the vertical direction:
02 |
android:orientation="vertical"> |
05 |
android:layout_width="wrap_content" |
06 |
android:layout_height="0dp" |
07 |
android:layout_weight="1" |
11 |
android:layout_width="wrap_content" |
12 |
android:layout_height="0dp" |
13 |
android:layout_weight="1" |
14 |
android:text="999999"/> |
Layout_weight can be used in this way.