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值的和。
公众号【一个码农的日常】 技术群:319931204 1号群: 437802986 2号群: 340250479
出处:http://zhangs1986.cnblogs.com/
码云:https://gitee.com/huanzui
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?