位置: 编程技术 - 正文

LinearLayout layout_weight解析

编辑:rootadmin

推荐整理分享LinearLayout layout_weight解析,希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

LinearLayout是Android布局中经常使用的一种布局,layout_weight属性经在Linearlayout布局中常使用,至于layout_weight的宽度是怎样计算出来的,只有了解了其中的原理,才能真正了解layout_weight的宽度到底是怎么样确定的。

以下是Linearlayout的layout_weight计算原理(只针对orientation为horizontal讲解,vertical的原理相同)。

layout_weight的宽度计算主要分为两种:

1、子View 的宽度都为wrap_contet 类型

三个LinearLayoutLA、LB、LC的都是 layout_width="wrap_content"时,weight取&#;分别为1、2、3,会得到以下效果:

<?xmlversion="1.0"encoding="UTF-8"?>

<LinearLayoutxmlns:android="

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal"

android:background="#FF">

<LinearLayout

android:id="@&#;id/LA"

android:orientation="horizontal"

android:background="#ffff"

android:textColor="@android:color/white"

android:layout_width="wrap_content"

android:layout_height="px"

android:layout_weight="1"/>

<LinearLayout

android:id="@&#;id/LB"

android:orientation="horizontal"

android:background="#ffcccccc"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="2">

<TextView

android:background="#ffcccccc"

android:layout_width="px"

android:layout_height="px"

android:text="2"

android:textColor="@android:color/black"

android:textSize="sp"/>

</LinearLayout>

<LinearLayout

android:id="@&#;id/LC"

android:orientation="horizontal"

android:background="#ffddaacc"

android:textColor="@android:color/black"

android:layout_width="wrap_content"

android:layout_height="px"

android:layout_weight="3"/>

</LinearLayout>

系统先给3个LinearLayout分配他们的宽度&#;wrap_content(宽度足以包含他们的内容1,2,3即可),然后会把剩下来的屏幕空间按照1:2:3的比列分配给3个LinearLayout,所以就出现了上面的图像。

计算原理:LB中有一个TextView宽度为px,所以会先给LB分px,然后把剩下的宽度按比例分给LA、LB、LC

LA的宽度 = ( - ) / (1 &#;2 &#;3) * 1

LB的宽度 = ( - ) / (1 &#;2 &#;3) * 2 &#;

LC的宽度 = ( - ) / (1 &#;2 &#;3) * 3

注:如果子View设有padding或者margin,先把padding或者margin去掉,然后按照上述的逻辑进行计算

如:

<?xmlversion="1.0"encoding="UTF-8"?>

<LinearLayoutxmlns:android="

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal"

android:background="#FF">

<LinearLayout

android:id="@&#;id/LA"

android:orientation="horizontal"

android:background="#ffff"

android:textColor="@android:color/white"

android:layout_marginLeft="px"

android:layout_width="wrap_content"

android:layout_height="px"

android:layout_weight="1"/>

<LinearLayout

android:id="@&#;id/LB"

android:orientation="horizontal"

android:background="#ffcccccc"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="2">

<TextView

android:background="#ffcccccc"

android:layout_width="px"

android:layout_height="px"

android:text="2"

android:textColor="@android:color/black"

android:textSize="sp"/>

</LinearLayout>

<LinearLayout

android:id="@&#;id/LC"

android:orientation="horizontal"

android:background="#ffddaacc"

android:textColor="@android:color/black"

android:layout_width="wrap_content"

android:layout_height="px"

android:layout_weight="3"/>

</LinearLayout>

计算原理:LA设置了marginLeftpx, LB中有一个TextView宽度为px,所以会先把LA的marginLeft去掉,其次给LB分px,然后把剩下的宽度按比例分给LA、LB、LC

LA的宽度 = ( - - ) / (1 &#;2 &#;3) * 1 = ,marginLeft为

LB的宽度 = ( - - ) / (1 &#;2 &#;3) * 2 &#; =

LC的宽度 = ( - - ) / (1 &#;2 &#;3) * 3 =

同理:padding也是同样的计算方法

2、子View 的宽度都为fill_parent 类型

在使用fill_parent时,实际发现权重小,反而分的越多,这是为什么呢???网上很多人说是当layout_width= "fill_parent" 时,weight&#;越小权重越大,优先级越高,但并不清除其中的具体计算规则。

三个LinearLayoutLA、LB、LC的都是 layout_width="fill_content"时,weight取&#;分别为1、2、3,会得到以下效果:

LinearLayout layout_weight解析

<?xmlversion="1.0"encoding="UTF-8"?>

<LinearLayoutxmlns:android="

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal"

android:background="#FF">

<LinearLayout

android:id="@&#;id/LA"

android:orientation="horizontal"

android:background="#ffff"

android:textColor="@android:color/white"

android:layout_width="fill_parent"

android:layout_height="px"

android:layout_weight="1"/>

<LinearLayout

android:id="@&#;id/LB"

android:orientation="horizontal"

android:background="#ffcccccc"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="2">

<TextView

android:background="#ffcccccc"

android:layout_width="px"

android:layout_height="px"

android:text="2"

android:textColor="@android:color/black"

android:textSize="sp"/>

</LinearLayout>

<LinearLayout

android:id="@&#;id/LC"

android:orientation="horizontal"

android:background="#ffddaacc"

android:textColor="@android:color/black"

android:layout_width="fill_parent"

android:layout_height="px"

android:layout_weight="3"/>

</LinearLayout>

计算原理如下:

系统先给3个View分配他们所要的宽度fill_parent,也就是说每一都是填满他的父控件,这里就是屏幕的宽度

那么这时候的剩余空间=1个parent_width-3个parent_width=-2个parent_width (parent_width指的是屏幕宽度 )

LA的实际所占宽度

LA Width = fill_parent的宽度 &#; 他所占剩余空间的权重比列

即:LA Width = parent_width &#; 1/6 * ( -2 parent_width) = 2/3 parent_width

LB的实际所占宽度

LB Width = parent_width &#; 2/6*(-2parent_width) = 1/3parent_width;

LC的实际所占宽度 LC Width = parent_width &#; 3/6*(-2parent_width) =0;

所以就是2:1:0的比列显示了。

这样你也就会明白为什么当你把三个Layout_weight设置为1、2、3的话,会出现上面的效果了:

注:如果子View设有padding或者margin,然后按照上述的逻辑进行计算,然后再把padding或者margin从分得的宽度中去掉,

如:

<?xmlversion="1.0"encoding="UTF-8"?>

<LinearLayoutxmlns:android="

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal"

android:background="#FF">

<LinearLayout

android:id="@&#;id/LA"

android:orientation="horizontal"

android:background="#ffff"

android:textColor="@android:color/white"

android:layout_marginLeft="px"

android:layout_width="fill_parent"

android:layout_height="px"

android:layout_weight="1"/>

<LinearLayout

android:id="@&#;id/LB"

android:orientation="horizontal"

android:background="#ffcccccc"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="2">

<TextView

android:background="#ffcccccc"

android:layout_width="px"

android:layout_height="px"

android:text="2"

android:textColor="@android:color/black"

android:textSize="sp"/>

</LinearLayout>

<LinearLayout

android:id="@&#;id/LC"

android:orientation="horizontal"

android:background="#ffddaacc"

android:textColor="@android:color/black"

android:layout_width="fill_parent"

android:layout_height="px"

android:layout_weight="3"/>

</LinearLayout>

3、总结

layout_weight并不是view的最终宽度(或者高度)的百分比,而是根据一定的规则进行计算出来的,在进行界面布局的时候一定要注意这一点。

另外:一些自定义的View中使用layout_weight可能达到的效果和上面讲述的不相同,因为自定义的VIew可能修改了View的onLayout和onMeasure函数,导致界面元素宽度计算有问题

部分内容参考以下网站

版权声明:本文为博主原创文章,未经博主允许不得转载。

Java项目的*.java 文件打开后注释乱码问题解决办法。 方法很简单,到项目的src文件下,右键选中任意一个.java文件然后选择打开方式,里面会有写字板和文本文件连个选项然后选中文本文件,这样就可以了

在Android中解析XML 在Android中解析XML知识点概述:1.DOM解析XML2.SAX解析XML3.Pull解析XML知识点详述:前序:XML是可扩展性标记语言,可以自己定义标签.在android中处理xml数据很常见,

Eclipse ctrl+shift+r 总所周知,Eclipsectrlshiftr可以查找你想要的文件,并且你可以通过preference设置,将你不想查询出来的信息过滤。相信大家和我一样,都遇到一个问题,

标签: LinearLayout layout_weight解析

本文链接地址:https://www.jiuchutong.com/biancheng/380949.html 转载请保留说明!

上一篇:PullScrollView详解(三)——PullScrollView实现(pulls toward)

下一篇:Java项目的*.java 文件打开后注释乱码问题解决办法。(java项目的命名规则)

  • 自己房子出租发布信息
  • 上年少计提工会经费本年怎么调整
  • 预付的成本票计入什么科目?
  • 增值税发票是真发票,但是平台查验不到
  • 2020年小规模纳税人增值税起征点
  • 工程行业存货哪些科目组成
  • 成品油红字发票开错了
  • 单一环节征税有哪些类型
  • 员工宿舍买热水器计入什么费用
  • 青年企业家协会入会要求
  • 如何分清进口农产品增值税抵扣率
  • 免税项目怎么做账
  • 出口企业免抵税额在增值税申报表填写附表5
  • 汽车减税
  • 固定收益款可以开增值税专票吗?
  • 年度所得税汇算清缴报告在哪打印
  • 房屋及建筑物原值计算
  • 委托、受托出口做账有哪些事项?
  • 本月没开票怎么报税
  • 公司没有缴纳住房公积金离职能要求补缴吗
  • 增值税发票专票有效期
  • 注册资金怎么提出来
  • 一般空调安装费多少钱
  • 建筑企业预收的工程款会计分录
  • 物流车辆挂靠账务怎么处理?
  • etc充值发票可以抵扣税吗
  • 冲销暂估购入原材料的账务处理?
  • 累计折旧是备抵类科目吗
  • 资产处置收益计入哪个会计科目
  • 水利建设基金从铁路建设基金港口建设费收入中提取5%
  • 日工资计算的三种方法
  • 附加税包括哪些税种2021年
  • 票据质押后如何行使质权
  • 条形码费用属于哪个科目
  • 差额征税的计算方式
  • 研发费加计扣除申报表怎么填
  • 关于幼儿园会没课程的会刊
  • win10怎么设置win7开始菜单
  • PHP:xml_set_unparsed_entity_decl_handler()的用法_XML解析器函数
  • 咨询服务费怎么报印花税
  • 所得税季报填报说明
  • 合法票据的有哪些
  • 债券转换是什么意思
  • 劳务所得和经营所得有啥区别
  • php pdo sqlserver
  • nginx安装配置视频
  • 用python编写素数
  • 水费发票掉了可以补打吗
  • 注册公司行业分类有多少种
  • 当月开的增值税发票当月能抵扣吗
  • 第9章 内存模型分析
  • 织梦配置文件
  • 摄影属于哪种服务业
  • 小型生产加工企业税率
  • 怎么盘存货
  • 财务软件是有哪些软件
  • 失控发票进项税额结转成本
  • 应付账款收不回发票该如何调整
  • 对外担保的效力
  • 长期股权投资损益调整是什么意思
  • 小型微利企业预缴企业所得税怎么算
  • 单位购买金税盘账务处理
  • 进项税额转出在哪里查
  • 不动产经营租赁属于什么服务
  • 现金支票办理电话
  • mysql数据库无法连接到服务器怎么办
  • windows怎么用
  • win10右键菜单自定义
  • ctrl+的作用
  • ct.dll
  • Win7系统启动盘
  • windows8装 .NET 3.5 时出现0x800F081F错误解决方法
  • WIN10开始菜单点不动
  • Expand、Fasthelp、Fc命令的用法
  • 简介英文
  • unity 2Dtoolkit 插件创建中文字体
  • div的class有空格
  • 三证合一,税号和营业执照不一样可以吗
  • 浙里办扫码在哪里
  • 增值税发票不够用怎么增票
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设