Detailed explanation of the weight usage of linear layout

Understanding of weights in linear layout
Please indicate the source: http://renyuan-1991.iteye.com/blog/2272200
Today, I suddenly thought of my understanding of linear layout, so I want to record it here. Before writing this blog, I deliberately read a lot of linear layout weights written by others and found that only one is correct, and the rest are according to "the larger the weight when wrapping the content, the greater the proportion, and the greater the weight when matching the parent form, the greater the proportion. "small", or what law is inversely proportional. This kind of understanding is wrong, and it will cause great trouble to write dynamic layout by yourself. If you follow this understanding, the following situation cannot be explained.
<LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        android:layout_height="50dp">
        <TextView
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:background="@color/blue"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="2"
            android:layout_width="match_parent"
            android:background="@color/red"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="3"
            android:background="@color/yellow"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

According to the above statement, the style displayed by this code should be 3:2:1, but the actual situation is the second linear layout in the figure below. The yellow textview in the layout displayed by the

above code is not displayed at all, so "when wrapping the content The bigger the weight, the bigger the proportion, the bigger the weight and the smaller the proportion when matching the parent form." This statement is incorrect. The final width of the control = the initial width of the control + ((the width of the screen - the sum of the width of the control) / (the sum of all weights)) *Here is the horizontal direction, so the width is used as an example, and the same is true for the vertical direction.
Next, let's take a look at the corresponding code in the figure above
<LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        android:layout_height="50dp">
        <TextView
            android:layout_weight="1"
            android:layout_width="0dp"
            android:background="@color/blue"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="2"
            android:layout_width="0dp"
            android:background="@color/red"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="3"
            android:background="@color/yellow"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        android:layout_height="50dp">
        <TextView
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:background="@color/blue"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="2"
            android:layout_width="match_parent"
            android:background="@color/red"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="3"
            android:background="@color/yellow"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        android:layout_height="50dp">
        <TextView
            android:layout_weight="1"
            android:layout_width="0dp"
            android:background="@color/blue"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="1"
            android:layout_width="0dp"
            android:background="@color/red"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="1"
            android:background="@color/yellow"
            android:layout_width="100dp"
            android:layout_height="match_parent" />
    </LinearLayout>

The first piece of code: the sum of the widths of the controls is 0dp, so the remaining amount of the screen is 1 screen width. The sum of weights is 6. First divide the remaining amount of the screen into 6 parts, the first TextView occupies one part, the second TextView occupies two parts, and the third TextView occupies three parts. In the end they show a width of 0+1/6, 0+2/6, 0+3/6.
The second piece of code: The sum of the width of the controls is 3 screen widths, so the remaining amount of the screen is -2 screen widths. The sum of weights is 6. First divide the remaining amount of the screen into 6 parts, the first TextView occupies one part, the second TextView occupies two parts, and the third TextView occupies three parts. In the end, their displayed width is 1+(1/6)*(-2), 1+(2/6)*(-2), 1+(3/6)*(-2). Therefore, the first TextView and the second TextView have already filled the screen, and the
third . The sum of weights is 6. First divide the remaining amount of the screen into 6 parts, the first TextView occupies one part, the second TextView occupies two parts, and the third TextView occupies three parts. Finally, the width they display is 0+(1/6)*remaining, 0+(2/6)*remaining, 100+(3/6)*remaining
ok, and that's it for now...


hope Friends who like programming can join this group, help each other, and learn together. Group number: 141877583

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327064050&siteId=291194637