Understanding of Android LinearLayout weights

Understanding of Android LinearLayout weights

normal mode

For example, in the case of android:orientation="vertical", android:layout_height="0dp" of all controls.
Then divide it into three equal parts according to the normal weight distribution 1:1:1, and each control accounts for 1/3

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_height="wrap_contentp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/upload"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="upload" />

        <Button
            android:id="@+id/download"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="download" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/uploadimg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            tools:srcCompat="@tools:sample/avatars" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:gravity="center">

        <ImageView
            android:id="@+id/downloadimg"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            tools:srcCompat="@tools:sample/avatars" />
    </LinearLayout>


</LinearLayout>

insert image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/upload"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="upload" />

        <Button
            android:id="@+id/download"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="download" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/uploadimg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            tools:srcCompat="@tools:sample/avatars" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:gravity="center">

        <ImageView
            android:id="@+id/downloadimg"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            tools:srcCompat="@tools:sample/avatars" />
    </LinearLayout>


</LinearLayout>

insert image description here

special mode

If set to android:layout_height="match_parent"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/upload"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="upload" />

        <Button
            android:id="@+id/download"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="download" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/uploadimg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            tools:srcCompat="@tools:sample/avatars" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:gravity="center">

        <ImageView
            android:id="@+id/downloadimg"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            tools:srcCompat="@tools:sample/avatars" />
    </LinearLayout>


</LinearLayout>

Then it is necessary to calculate the weight of the distribution. In general, the greater the weight, the less points

分成41:1:2
如果都是 match_parent 的话 
那么 3个控件 都要占一个屏,一共需要3个屏,但是现在只有1个屏可以用于分配  所以就有 1-3 = -2个屏   理解为-2个屏需要三个控件共同去减少自己一个屏的范围来承担
这-2个屏 需要三兄弟按照比重承担分别是  -2*(1/4)  -2*(1/4) -2*(1/2)
那么就有
本来就要占的一个屏 - 要承担比重的屏 = 真正占用的屏
1 -2*(1/4) = 1/2
1 -2*(1/4) = 1/2
分完就 没了

分成 1:2:3 呢 
那就是
1- 2*(1/6) = 2/3
1- 2*(2/6) = 1/3
分完就 没了

分成 1:2:2呢
那就是
1- *2(1/5) = 3/5
1- *2(2/5) = 1-4/5 = 1/5
那么剩下的就只能占 1/5

Guess you like

Origin blog.csdn.net/qq_36535153/article/details/131053522