layout_weight详解

简介:

注:LinearLayout中的TextView按比例显示的时候,layout_width="0dp"或者layout_height="0dp"
 在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示。android并没用提供table这样的控件,虽然有TableLayout,但是它并非是我们想象中的像html里面的table那么好用,我们常用ListView实现table的效果,但是列对齐确比较麻烦,现在用LinearLayout及属性android:layout_weight能很好地解决。下面我们共同体验下layout_weight这个属性。
一、LinearLayout内的控件的layout_width设置为"wrap_content"
请看一下xml配置:

复制代码
 1  <LinearLayout
 2       android:orientation="horizontal"
 3       android:layout_width="fill_parent"
 4       android:layout_height="fill_parent"
 5       android:layout_weight="1"
 6      >
 7       <TextView
 8           android:layout_width="wrap_content"
 9           android:layout_height="fill_parent"
10           android:layout_weight="1"
11           android:background="#aa0000"
12           android:gravity="center"
13           android:text="1"/>
14       <TextView
15           android:layout_width="wrap_content"
16           android:layout_height="fill_parent"
17           android:layout_weight="2"
18           android:background="#00aa00"
19           android:gravity="center"
20           android:text="1"/>
21       <TextView
22           android:layout_width="wrap_content"
23           android:layout_height="fill_parent"
24           android:layout_weight="3"
25           android:background="#0000aa"
26           android:gravity="center"
27           android:text="1"/>
28   </LinearLayout>
复制代码

效果如下:
若水工作室
可以看到这三个TextView是按照1:2:3的比例进行显示的,这样看来似乎可以实现按照比例显示了,但是有个问题,如果TextView内的文本长度一同那么较长文本的TextView会宽度会有所增加,见下面配置及效果:
配置:

复制代码
 1  <LinearLayout
 2       android:orientation="horizontal"
 3       android:layout_width="fill_parent"
 4       android:layout_height="fill_parent"
 5       android:layout_weight="1">
 6       <TextView
 7           android:layout_width="wrap_content"
 8           android:layout_height="fill_parent"
 9           android:layout_weight="1"
10           android:background="#aa0000"
11           android:gravity="center"
12           android:text="1111111111111111111111111111111111111111111"/>
13       <TextView
14           android:layout_width="wrap_content"
15           android:layout_height="fill_parent"
16           android:layout_weight="2"
17           android:background="#00aa00"
18           android:gravity="center"
19           android:text="2"/>
20       <TextView
21           android:layout_width="wrap_content"
22           android:layout_height="fill_parent"
23           android:layout_weight="3"
24           android:background="#0000aa"
25           android:gravity="center"
26           android:text="3"/>
27   </LinearLayout>
复制代码

效果:
若水工作室
这样看来我们所需要的按比例又无法实现了,经过满天地google终于找到了解决方案,就是设置layout_width设置为"wrap_content"。配置及效果见下:

复制代码
 1  <LinearLayout
 2       android:orientation="horizontal"
 3       android:layout_width="fill_parent"
 4       android:layout_height="fill_parent"
 5       android:layout_weight="1">
 6       <TextView
 7           android:layout_width="0dp"
 8           android:layout_height="fill_parent"
 9           android:layout_weight="1"
10           android:background="#aa0000"
11           android:gravity="center"
12           android:text="1111111111111111111111111111111111111111111"/>
13       <TextView
14           android:layout_width="0dp"
15           android:layout_height="fill_parent"
16           android:layout_weight="2"
17           android:background="#00aa00"
18           android:gravity="center"
19           android:text="2"/>
20       <TextView
21           android:layout_width="0dp"
22           android:layout_height="fill_parent"
23           android:layout_weight="3"
24           android:background="#0000aa"
25           android:gravity="center"
26           android:text="3"/>
27   </LinearLayout>
复制代码

效果:
若水工作室
这样终于达到我们的按比例显示的效果了,感觉很是奇怪,android开发框架的大佬们有时候设计确实有点匪夷所思。
二、LinearLayout内的控件的layout_width设置为"fill_parent"
请看一下xml配置:

复制代码
 1  <LinearLayout
 2       android:orientation="horizontal"
 3       android:layout_width="fill_parent"
 4       android:layout_height="fill_parent"
 5       android:layout_weight="1">
 6       <TextView
 7           android:layout_width="fill_parent"
 8           android:layout_height="fill_parent"
 9           android:layout_weight="1"
10           android:background="#aa0000"
11           android:gravity="center"
12           android:text="1"/>
13       <TextView
14           android:layout_width="fill_parent"
15           android:layout_height="fill_parent"
16           android:layout_weight="2"
17           android:background="#00aa00"
18           android:gravity="center"
19           android:text="2"/>
20   </LinearLayout>
复制代码

效果如下:

奇怪吧,整个宽度平分3块,第一个TextView占了两块,这样看来weight值越小的优先级越大。只有两个TextView似乎看出些道理,那么让我们看看三个是什么效果:

复制代码
 1 <LinearLayout
 2       android:orientation="horizontal"
 3       android:layout_width="fill_parent"
 4       android:layout_height="fill_parent"
 5       android:layout_weight="1">
 6       <TextView
 7           android:layout_width="fill_parent"
 8           android:layout_height="fill_parent"
 9           android:layout_weight="1"
10           android:background="#aa0000"
11           android:gravity="center"
12           android:text="1"/>
13       <TextView
14           android:layout_width="fill_parent"
15           android:layout_height="fill_parent"
16           android:layout_weight="2"
17           android:background="#00aa00"
18           android:gravity="center"
19           android:text="2"/>
20       <TextView
21           android:layout_width="fill_parent"
22           android:layout_height="fill_parent"
23           android:layout_weight="3"
24           android:background="#0000aa"
25           android:gravity="center"
26           android:text="3"/>
27   </LinearLayout>
复制代码

效果:

什么意思?第三个TextView丢掉了,很是奇怪,让我们再试一个,把weight分别改为2,3,4的看看效果:
若水工作室
这个效果让人很困惑,我一直想寻求出一个确切的比例公式,但是至今未找到。有哪位大神能搞定的话忘不吝赐教。
虽然这个android:layout_weight属性很怪异,但幸运的是我们达到了目标:
按比例显示LinearLayout内各个子控件,需设置android:layout_width="0dp",如果为竖直方向的设置android:layout_height="0dp"。在这种情况下某子个控件占用LinearLayout的比例为:本控件weight值 / LinearLayout内所有控件的weight值的和。

本文转自欢醉博客园博客,原文链接http://www.cnblogs.com/zhangs1986/p/3248187.html如需转载请自行联系原作者


欢醉

相关文章
|
缓存 网络协议 前端开发
CDN最佳实践之访问慢的分析思路和优化方案
使用CDN加速以后还是存在访问慢的情况,如何去分析定位问题、优化网站速度、解决用户问题是一个十分重要的课题。本文介绍了CDN加速访问慢的分析思路,通过归纳的一些原因结合搜集的信息去进一步判断定位问题,帮助用户在遇到问题时有一个更清晰的思考方法论。同时介绍了一些典型的问题场景,结合这些问题场景可以更快速的去发现问题并优化。
2883 1
CDN最佳实践之访问慢的分析思路和优化方案
|
中间件 Java 应用服务中间件
工行银企互联接入详解(4)--NC中间件部署
本文目录 1. 背景 2. 原理 3. 声明 4. 部署流程 4.1 下载并解压NC中间件 4.2 修改Web.Config文件,如下图: 4.3 启用IIS功能 4.4 在IIS管理器中添加网站 4.5 测试中间件是否部署成功 4.6 测试中间件参数是否正确 5. 总结
540 74
工行银企互联接入详解(4)--NC中间件部署
|
资源调度 Kubernetes 监控
如何可视化编写和编排你的k8s任务
阿里任务调度SchedulerX和云原生结合,重磅推出可视化k8s任务,针对脚本使用者,屏蔽了容器服务的细节,不用构建镜像就可以让不熟悉容器的同学(比如运维和运营同学)玩转k8s Job,受益容器服务带来的降本增效福利。针对容器使用者,SchedulerX不但完全兼容原生的k8s Job,还能支持历史执行记录、日志服务、重跑任务、报警监控、可视化任务编排等能力,为企业级应用保驾护航
1601 1
|
弹性计算 大数据 BI
阿里云ACP认证哪个值得考?考试时间怎么安排?
对于现在的人来说,网络就是帮助自己了解世界的好帮手、就是让自己生活得更方便的好工具,这样一来,市场就需要大量的人才来满足需求,相对应的岗位也逐渐增多。
阿里云ACP认证哪个值得考?考试时间怎么安排?
|
开发框架 人工智能 算法
游戏人工智能——追逐与拦截
(1)描述追逐与拦截的算法原理:连续环境中的视线追逐是最简单的追逐算法,但是追逐者的移动不仅有线速度,而且还有角速度。算法思路就是:首先根据角速度把方向转到视线方向,然后向目标追过去。完整追逐/闪躲由三部分组成:首先,作出追或逃的决策判断。其次,开始追或逃(本章重点)最后,避开障碍物。拦截算法的基本原理是可以预测猎物未来的位置,然后直接到那个位置去,让追击者和猎物同时到达同一个位置。为了找出追击者和猎物能同时到达的点,不仅要考虑他们的移动方向,还要考虑他们的速度。 (2)Main和winmain进行函数的定义。RigidBody2D类:进行物体的质量,惯性,关系,坐标,长高宽即外形的定义,用
457 0
游戏人工智能——追逐与拦截
|
存储 C++
3.2操作系统(基本分页存储管理的基本概念)
💧什么是分页存储 🍓如何实现地址的转换 🍒逻辑地址结构 🥦重要的数据结构——页表
3.2操作系统(基本分页存储管理的基本概念)
|
传感器 IDE 物联网
HaaS EDU场景式应用学习 - 光照信息屏
HaaS EDU场景式应用学习 - 光照信息屏
HaaS EDU场景式应用学习 - 光照信息屏
|
前端开发 算法 安全
Java8新特性 十二大总结 (面试篇)
Java8新特性 十二大总结 (面试篇)
880 0
Java8新特性 十二大总结 (面试篇)
|
机器学习/深度学习 人工智能 自然语言处理
智能虚拟助理如何助你在 2022 年成倍提高工作效率
它们可以帮你订比萨,找到优惠票价,并让你意识到娱乐花销要比投资更多。我们说的正是人工智能虚拟助理,它们如今已成为我们日常生活中熟悉的一部分。但是,人工智能助理背后有哪些技术,你又如何在业务中使用它们呢?本文将逐一解答。
智能虚拟助理如何助你在 2022 年成倍提高工作效率
|
JSON SpringCloudAlibaba 算法
微服务场景实战:基于SpringCloud Alibaba从零搭建鉴权中心服务(1)
微服务场景实战:基于SpringCloud Alibaba从零搭建鉴权中心服务(1)
微服务场景实战:基于SpringCloud Alibaba从零搭建鉴权中心服务(1)