android中layout_weight的理解

例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#00ff00"
    android:weightSum="0"
    android:orientation="horizontal" >

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

    <Button
        android:id="@+id/imageViewLoginState1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="2" >
    </Button>

    <Button
        android:id="@+id/imageViewLoginState2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:text="3" >
    </Button>

</LinearLayout>

假设Container即LinearLayout的宽度为PARENT_WIDTH

三个按钮的宽度都是FILL_PARENT,所以在应用layout_weight之前,三个按钮的宽度都为PARENT_WIDTH

所以额外空间: delta = PARENT_WIDTH - 3 * PARENT_WIDTH = -2 * PARENT
因为LinearLayout没有设置android:weightSum(默认为0,设置为0就当没设置吧),所以 mWeightSum = 1 + 1 +2 =4

所以:

第一个按钮的宽度为:
PARENT_WIDTH + share = PARENT_WIDTH + (layout_weight * delta / mWeightSum)

                                            = PARENT_WIDTH + (1 * (-2 * PARENT_WIDTH) /4)                         = 1 /2 *PARENT_WIDTH
                                            然后更新weightSum与delta:

weightSum -= childExtra;(=3)
delta -= share;(=-3/2 * PARENT_WIDTH)
第二个按钮的宽度为:ARENT_WIDTH + share = PARENT_WIDTH + (layout_weight * delta / mWeightSum)

                                            = PARENT_WIDTH + (1 * (-3 / 2 * PARENT_WIDTH) /3)                                 = 1 /2 *PARENT_WIDTH
                                                                                    更新weightSum与delta:

weightSum -= childExtra;(=2)
delta -= share;(=-PARENT_WIDTH)
第三个按钮的宽度为:
PARENT_WIDTH + share = PARENT_WIDTH + (layout_weight * delta / mWeightSum) = PARENT_WIDTH + (2 * (- PARENT_WIDTH) /2) = 0
所以最终的而已就是前两个按钮平分LinearLayout,第三个按钮消失了.
举例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:background="#00ff00"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="60dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="1" >
    </Button>

    <Button
        android:id="@+id/button2"
        android:layout_width="60dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="2" >
    </Button>

    <Button
        android:id="@+id/button3"
        android:layout_width="60dp"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:text="3" >
    </Button>

</LinearLayout>

额外空间:delta = 100- 3 * 60 = -80
mWeightSum = 1 + 1 +2 =4

所以:
第一个按钮的宽度为:60+ share = 60 + (layout_weight * delta / mWeightSum) = 60 + (1 * (-80) /4) = 40

然后:weightSum -= childExtra;(=3)
delta -= share;(=-60)
第二个按钮的宽度为:0 + share = 60 + (layout_weight * delta / mWeightSum) = 60 + (2 * (-60) /3)= 40
然后:weightSum -= childExtra;(=2)
delta -= share;(=-40)

第三个按钮的宽度为:
60 + share = 60 + (layout_weight * delta / mWeightSum)
= 60 + (2 * (-40) /2)
= 20

例外:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:background="#00ff00"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="60dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="1" >
    </Button>

    <Button
        android:id="@+id/button2"
        android:layout_width="40dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="2" >
    </Button>


</LinearLayout>

额外空间为100,所以Button1的宽度为60+100/2=110,Button2的宽度为40+100/2=90。

参考:http://www.cnblogs.com/angeldevil/archive/2012/04/08/2437747.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值