layout_weight如何计算比例?

本文详细解析了LinearLayout中Button布局宽度与权重的关系,包括wrap_content和fill_parent两种情况下的显示原理及解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一。当LinearLayout中的Button的layout_width="wrap_content"时

          一切没问题,也很容易理解,btn1和btn2和btn3宽度的比就是1:2:3也就是weight的比


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">
    
    <Button
        android:id="@+id/btn1"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:text="1">
    </Button>
    <Button 
        android:id="@+id/btn2"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:layout_weight="2"
        android:text="2">
    </Button>
     <Button 
        android:id="@+id/btn3"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:layout_weight="3"
        android:text="3">
    </Button>
</LinearLayout>



    当然如果你的Button的内容太长了,长得你的weight=1满足不了“layout_width=“”wrap_content”就会出现这样:


  
    <Button
        android:id="@+id/btn1"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:text="11111111111111111111111">


解决方案:将button属性改为layout_width=”0dp“,原因是layout_height和layout_width的优先级大于layout_weight,如果layout_width="wrap_content",button就会去先满足wrap_content!~~~但你layout_width=”0dp“时不设置weigth时会报错的:Suspicious size: this will make the view invisible, should be used with layout_weight


  
    <Button
        android:id="@+id/btn1"
        android:layout_height="fill_parent"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:text="11111111111111111111111">
    </Button>


二。当LinearLayout中的Button的layout_width="fill_parent"时


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">
    
    <Button
        android:id="@+id/btn1"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:text="1">
    </Button>
    <Button 
        android:id="@+id/btn2"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="2"
        android:text="2">
    </Button>
     <Button 
        android:id="@+id/btn3"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="3"
        android:text="3">
    </Button>
</LinearLayout>



咦?btn3哪去了?来看看weight的计算公式吧,因为btn1、2、3都是layout_width=”fill_parent“,那么就少了2个parent_width啊~~(如果没有这weight,这时应是btn1布满屏幕),"之前的如果分配为fill_parent的话把,剩余的为1个parent_width (parent_width指的是屏幕宽度 )-3个parent_width=(-2个parent_width ),再按比例分配 :btn1所占空间=1个parent_width+1/6 *(-2个parent_width)=(2/3个parent_width)

btn2:1个parent_width+2/6 *(-2个parent_width)=(1/3个parent_width)"


这样一来btn1和btn2就已经占了2/3+1/3=1个parent_width了,所以就看不见btn3了

改变一下weight的比,,再看

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">
    
    <Button
        android:id="@+id/btn1"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="2"
        android:text="1">
    </Button>
    <Button 
        android:id="@+id/btn2"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="3"
        android:text="2">
    </Button>
     <Button 
        android:id="@+id/btn3"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="4"
        android:text="3">
    </Button>
</LinearLayout>

恩恩,就是这样,
总之:1.layout_width=”wrap_content“时,按照weight的比例显示(内容过长时--->layout_width=”0dp“)
            2.layout_width=”fill_parent“时,按照上面的公式算算比例就行

严重参考过的文章
点击打开链接

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值