Android Studio中的layout_weight属性讲解

属性含义

android:layout_weight的表面含义是:分配的比重值,默认值为0 。真实含义是:一旦View设置了该属性(假设有效的情况下),那么该View的宽度等于原有宽度(android:layout_width)加上剩余空间(宽度)的占比!

控件最终宽度计算公式

控件A最终宽度 = 控件A初始宽度 +(屏幕宽度 - 控件宽度和)* 控件A的weight的值 /所有weight之和。
下面分析中所提到的剩余宽度 = 屏幕宽度 - 控件宽度和

第一种

<?xml version="1.0" encoding="utf-8"?>
    <!-- LinearLayoutCompat其实就是LinerLayout组件,只是为了兼容低版本-->
<androidx.appcompat.widget.LinearLayoutCompat 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="horizontal"
    tools:context=".MainActivity">
    <!--第一种-->
    <Button
        android:id="@+id/button_0"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/blue"
     />

    <Button
        android:id="@+id/button_1"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/red"
       />

    <Button
        android:id="@+id/button_2"
        android:layout_weight="3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/yellow"
        />
</androidx.appcompat.widget.LinearLayoutCompat>
    <!--
    android:layout_weight的真实含义是:一旦View设置了该属性(假设有效的情况下),那么该View的宽度等于原有宽度(android:layout_width)加上剩余空间的占比!
    计算公式:
    控件A最终宽度 = 控件A初始宽度+(屏幕宽度 - 控件宽度和)* 控件A的weight的值 /所有weight之和
    下面分析中所提到的剩余宽度 = 屏幕宽度 - 控件宽度和
    -->
    <!--
        设屏幕宽度为L,三个view指定的android:layout_width都是0,所以剩余宽度为L.
        三个TextView的宽度依次为:
        0+(L)*1/6
        0+(L)*2/6
        0+(L)*3/6
    -->

第二种

<?xml version="1.0" encoding="utf-8"?>
    <!-- LinearLayoutCompat其实就是LinerLayout组件,只是为了兼容低版本-->
<androidx.appcompat.widget.LinearLayoutCompat 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="horizontal"
    tools:context=".MainActivity">
    <!--第二种-->
    <Button
        android:id="@+id/button_0"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/blue"
        />

    <Button
        android:id="@+id/button_1"
        android:layout_weight="2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/red"
        />

    <Button
        android:id="@+id/button_2"
        android:layout_weight="3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/yellow"
        />

</androidx.appcompat.widget.LinearLayoutCompat>
    <!--
        设屏幕宽度为L,在三个view的宽度都为match_parent的情况下,原有宽度为L,三个的View的宽度都为L,剩余宽度为L-(L+L+L) = -2L
        三个TextView的宽度依次为:
        L+(-2L)*1/6 = (2/3)L
        L+(-2L)*2/6 = (1/3)L
        L+(-2L)*3/6 = (0)L
        这就是为什么layout_weight设置越大显示的占比反而越小,甚至是不显示的原因
        事实上默认的View的weight这个值为0,一旦设置了这个值,那么所在view在绘制的时候执行onMeasure两次的原因就在这。
    -->

第三种

<?xml version="1.0" encoding="utf-8"?>
    <!-- LinearLayoutCompat其实就是LinerLayout组件,只是为了兼容低版本-->
<androidx.appcompat.widget.LinearLayoutCompat 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="horizontal"
    tools:context=".MainActivity">

    <!--第三种-->
    <Button
        android:id="@+id/button_0"
        android:layout_weight="1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:background="@color/blue"
        />

    <Button
        android:id="@+id/button_1"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/red"
        />

    <Button
        android:id="@+id/button_2"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/yellow"
        />

</androidx.appcompat.widget.LinearLayoutCompat>

<!--
    设屏幕宽度为L,剩余宽度为L-(100dp)
    三个Button的宽度依次为:
    100+(L-(100dp))*1/3
    0+(L-(100dp))*1/3
    0+(L-(100dp))*1/3
    相当于是把L-100dp平均分为6份,然后按比重划分给每个view
    最后把100dp加到指定的view上
-->

第四种

<?xml version="1.0" encoding="utf-8"?>
    <!-- LinearLayoutCompat其实就是LinerLayout组件,只是为了兼容低版本-->
<androidx.appcompat.widget.LinearLayoutCompat 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="horizontal"
    tools:context=".MainActivity">

    <!--第四种-->
    <Button
        android:id="@+id/button_0"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:background="@color/black"
        />

    <Button
        android:id="@+id/button_1"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/blue"
        />

    <Button
        android:id="@+id/button_2"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/red"
        />

    <Button
        android:id="@+id/button_3"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/yellow"
        />

</androidx.appcompat.widget.LinearLayoutCompat>

<!--
    设屏幕宽度为L,剩余宽度为L-(100dp)
    三个Button的宽度依次为:
    100+(L-(100dp))*1/3
    0+(L-(100dp))*1/3
    0+(L-(100dp))*1/3
    相当于是把L-100dp平均分为6份,然后按比重划分给每个view
    最后把100dp加到指定的view上
-->

第五种

<?xml version="1.0" encoding="utf-8"?>
    <!-- LinearLayoutCompat其实就是LinerLayout组件,只是为了兼容低版本-->
<androidx.appcompat.widget.LinearLayoutCompat 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"
    tools:context=".MainActivity">

    <!--第五种-->
    <Button
        android:id="@+id/button_0"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:text="123456789123456789"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:background="@color/blue"

        />

    <Button
        android:id="@+id/button_1"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/red"
        />

    <Button
        android:id="@+id/button_2"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/yellow"
        />

</androidx.appcompat.widget.LinearLayoutCompat>

<!--
    第一个view的 android:layout_width=“wrap_content”,所以初始宽度为text的长度
    设屏幕宽度为L,剩余宽度为L-(text的长度)
    三个TextView的宽度依次为:
    text的长度+(L-(text的长度))*1/3
    0+(L-(text的长度))*1/3
    0+(L-(text的长度))*1/3
-->

第六种

<?xml version="1.0" encoding="utf-8"?>
    <!-- LinearLayoutCompat其实就是LinerLayout组件,只是为了兼容低版本-->
<androidx.appcompat.widget.LinearLayoutCompat 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"
    android:baselineAligned="false"
    tools:context=".MainActivity">

    <!--第六种-->
    <Button
        android:id="@+id/button_0"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:text="1233444556666777889"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:background="@color/blue"
        />

    <Button
        android:id="@+id/button_1"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/red"
        />

    <Button
        android:id="@+id/button_2"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/yellow"
        />

</androidx.appcompat.widget.LinearLayoutCompat>

<!--
    设屏幕宽度为L,三个view指定的android:layout_width都是0,所以剩余宽度为L.
    三个TextView的宽度依次为:
    0+(L)*1/3
    0+(L)*1/3
    0+(L)*1/3
-->

六种情况的效果图

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值