Layout_weight Analysis of Android layout
<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android" Xmlns:tools="Http://schemas.android.com/tools"android:layout_width="Match_parent" android:layout_height="Match_parent"android:orientation="Horizontal" > <textview android:layout _weight = "1" android:layout_width =< Span class= "Hljs-value" > "0DP" android:gravity = "center" android:background = "#ff6666" android:layout_height =" 50DP " android:text = "Hello world!" /> <textview android:layout _weight = "1" android:layout_width =< Span class= "Hljs-value" > "0DP" android:gravity = "center" android:layout_height = "50DP" android:background =" #448790 "/> <TextViewandroid:layout_weight="1"android:layout_width="0DP" android:layout_height="50DP"android:gravity="center"android: Background="#ff00ff" /> </linearlayout>
As shown in the code above, we usually use linearlayout to wrap the three TextView and then set the Layout_weight and Layout_width properties of each TextView to "1" and "0DP" respectively to achieve this split effect:
What if we want to show it in the 1:2:3 ratio? Let's change the code slightly:
<linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns:tools =" Http://schemas.android.com/tools " android:layout_width = "match_parent" android:layout_height =" match_parent " android:orientation =; <textview android:layout _weight = "1" android:layout_width =< Span class= "Hljs-value" > "0DP" android:gravity = "center" android:background = "#ff6666" android:layout_height =" 50DP " android:text = "Hello world! Hello world! " /> <textview android:layout _weight = "2" android:layout_width =< Span class= "Hljs-value" > "0DP" android:gravity = "center" android:layout_height = "50DP" android:background =" #448790 " android:text = "1" /> <TextViewandroid:layout_weight="3"android:layout_width="0DP" android:layout_height="50DP"android:gravity="center"Android:text ="2"android:background="#ff00ff" /> </linearlayout>
Do not change the layout_weight to 1, 2, 3 Well, so easy, however, and eggs, look at the results:
You will find that if the first textview of the copy line is OK, once more than one line will show a bit strange, what is the case? In fact, because of this linearlayout it has a baseline, you will find that the first line of text is on the same level, how to deal with this problem?
LinearLayout has a property baselinealigned attribute, set it to false.
android:baselineAligned="false"
Look at the results:
Change the code a little bit and set the first TextView layout_width to wrap_content:
<linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns:tools =" Http://schemas.android.com/tools " android:layout_width = "match_parent" android:layout_height =" match_parent " android:baselinealigned =" false " android:orientation = "horizontal" > <textview android:layout _weight = "1" android:layout_width =< Span class= "Hljs-value" > "wrap_content" android:gravity =" center " android:background =" # ff6666 " android:layout_height =" 50DP " android:text = "HFASDLSDHSLSKDJ" /> <textview android:layout _weight = "2" android:layout_width =< Span class= "Hljs-value" > "0DP" android:gravity = "center" android:layout_height = "50DP" android:background =" #448790 " android:text = "1" /> <TextViewandroid:layout_weight="3"android:layout_width="0DP" android:layout_height="50DP"android:gravity="center"Android:text ="2"android:background="#ff00ff" /> </linearlayout>
Look at the results:
Found that he would give priority to the width of the first TextView, and then the remaining width of the scale distribution, that is, the second TextView the remaining width of 2/5, the third TextView the remaining width of 3/5. Of course R If you set the width of the first textview to 100DP , the parent layout is also the width of the 100DP to the first TextView, and then the rest of the space to a proportion. The parent layout will subtract the sum of the widths of each sub-view, and then the rest is prorated.
Do not know whether you have to think of a problem, Android:layout_width, Android:layout_height, Android:layout_gravity, Android:gravity, Android: What is the difference between width, android:height and other attributes? In fact, the properties of the preceding layout are determined by the parent, which is equivalent to the parent layout of how much width, height, position to be on your side, and without layout of the property is determined content width, height, position.
For LinearLayout, there is also a android:weightsum attribute, which is used to specify the total weight, which exceeds the screen if the child view weighs more than the total weight. In some cases, the child view is set to a weight, but the individual child view display is not complete, you can set this property to solve. Because sometimes too fast layout will cause the parent to be too late to calculate, so the parent layout is pre-set the overall weight to avoid this situation.
Layout_weight Analysis of Android layout