android-weight属性的理解

  1. 当属性为match_parent时:weight属性可以理解为优先级;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="按钮一"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="按钮二"/>
</LinearLayout>

其效果图为:左图,如果将第一个button改为3,将变为右图所示

   170745_3jlN_2308371.png  170929_dqzF_2308371.png

由上面的代码分析,可以得出,如果属性定为mathch_parent时,它的weight越小,所占的空间也就越大,而且是成比例的增大。

2.当属性为warp_content时:

我们首先写出代码,看一下它的结果,我们再做分析:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3000"
            android:text="按钮一"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="按钮二"/>
</LinearLayout>

171733_ZY08_2308371.png

而且你会发现,一旦达到第二个按钮可以包括里面的text的时候,再增加button1的weight也无济于事,所以,对于这种情况,我们可以理解为,前提是最起码满足warp_content的条件,也就是要包含这个组件,然后才是按照比例分配空间。

3.看了上面两种情况,是不是感觉很麻烦,感觉都傻傻分不清楚,官方已经给了weight的正确使用方式:

In order to improve the layout efficiency when you specify the weight, you should change the width of theEditText to be zero (0dp). Setting the width to zero improves layout performance because using "wrap_content"as the width requires the system to calculate a width that is ultimately irrelevant because the weight value requires another width calculation to fill the remaining space.

也就是说,在使用时,要把对应的方向上的layout_width或者layout_height设置为0dp,这样的话呢,weight就成为了权重的意思,就可以完全根据比例来调整你的布局了。是不是瞬间就变的很简单而且使用了一个布局工具了。

下面我们简单的展示一下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:text="按钮一"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="按钮二"/>
</LinearLayout>

效果为:

191156_sE8r_2308371.png

转载于:https://my.oschina.net/ccqy66/blog/484783

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值