android:layout_weight属性你真的理解吗

大家看到这个标题可能会说,你这也太low了吧?权重这个属性谁不知道,谁没用过,有什么好讲的?是的,我确实经常用,但是我们并非真正的理解了它。ok,下面我们说说这一属性。

权重的平分(按比例划分):

这个确实没有什么好说的。就是我们最初对他的理解。所以这里我们跳过。

剩余空间按权重平分(重点):

这就是我想要说的,为了便于我们理解这里我结合布局代码讲解:

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="vertical"
 >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="35dp"
        android:background="#f00"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:textSize="35dp"
        android:background="#0f0"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:textSize="35dp"
        android:background="#00f"/>
</LinearLayout>
    <LinearLayout
        android:layout_marginTop="40dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="35dp"
            android:background="#f00"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:textSize="35dp"
            android:background="#0f0"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:textSize="35dp"
            android:background="#00f"/>
    </LinearLayout>

<LinearLayout
android:layout_marginTop="40dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="35dp"
    android:background="#f00"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:textSize="35dp"
    android:background="#0f0"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:textSize="35dp"
    android:background="#00f"/>
</LinearLayout>
</LinearLayout>

这是布局图:

这里写图片描述

首先我们要分三类讲解:
从布局文件大家可以看到我们是按照1:2:3做的权重分配
1. android:layout_width=”0dp”这个就是初学时候我们理解的权重分配
按照1:2:3做的权重分配布局。

2.android:layout_width=”wrap_content”首先系统会给3个view分配它们的宽度值,没错就是wrap_content,然后把剩余的宽度按照1:2:3再分配给他们,即1:2:3

3.android:layout_width=”match_parent”其实前两个很好理解,主要是第三个,同样系统会给3个view分配它们的宽度值就是match_parent,
接下来我们算一算剩余的宽度:

剩余的宽度=屏幕的宽度-3match_parent

你们看是不是这样,嗯,对的,理论上就是这样。那么也可以这样写:

剩余的宽度=match_parent-3match_parent=-2match_parent

so. 剩余的宽度=-2match_parent

好的,大家看下效果图是不是第三行的最后一个TextView不见了?现在我们就可以算下,他为什么消失咯,索性我们算下他们三个的比值。
(这里解释下,下面的计算:首先每个都会现分配一个match_parent,然后在平分剩余宽度,即 match_parent+剩余的宽度*所占的比例)

  • 第一个:
    match_parent+(-2)match_parent*1/6=2/3match_parent

  • 第二个:
    match_parent+(-2)match_parent*2/6=1/3match_parent

  • 第三个:
    match_parent+(-2)match_parent*3/6=0match_parent

好了,这就是我理解的权重,如果有错误,希望大家指出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的纺织品企业财务管理系统,源码+数据库+毕业论文+视频演示 在如今社会上,关于信息上面的处理,没有任何一个企业或者个人会忽视,如何让信息急速传递,并且归档储存查询,采用之前的纸张记录模式已经不符合当前使用要求了。所以,对纺织品企业财务信息管理的提升,也为了对纺织品企业财务信息进行更好的维护,纺织品企业财务管理系统的出现就变得水到渠成不可缺少。通过对纺织品企业财务管理系统的开发,不仅仅可以学以致用,让学到的知识变成成果出现,也强化了知识记忆,扩大了知识储备,是提升自我的一种很好的方法。通过具体的开发,对整个软件开发的过程熟练掌握,不论是前期的设计,还是后续的编码测试,都有了很深刻的认知。 纺织品企业财务管理系统通过MySQL数据库与Spring Boot框架进行开发,纺织品企业财务管理系统能够实现对财务人员,员工,收费信息,支出信息,薪资信息,留言信息,报销信息等信息的管理。 通过纺织品企业财务管理系统对相关信息的处理,让信息处理变的更加的系统,更加的规范,这是一个必然的结果。已经处理好的信息,不管是用来查找,还是分析,在效率上都会成倍的提高,让计算机变得更加符合生产需要,变成人们不可缺少的一种信息处理工具,实现了绿色办公,节省社会资源,为环境保护也做了力所能及的贡献。 关键字:纺织品企业财务管理系统,薪资信息,报销信息;SpringBoot
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值