layout_wigth详解
来源:互联网 发布:php隐藏域名跳转代码 编辑:程序博客网 时间:2024/04/30 21:31
注: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配置:
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" > <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background="#aa0000" android:gravity="center" android:text="1"/> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="2" android:background="#00aa00" android:gravity="center" android:text="1"/> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="3" android:background="#0000aa" android:gravity="center" android:text="1"/> </LinearLayout>
效果如下:
可以看到这三个TextView是按照1:2:3的比例进行显示的,这样看来似乎可以实现按照比例显示了,但是有个问题,如果TextView内的文本长度一同那么较长文本的TextView会宽度会有所增加,见下面配置及效果:
配置:
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background="#aa0000" android:gravity="center" android:text="1111111111111111111111111111111111111111111"/> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="2" android:background="#00aa00" android:gravity="center" android:text="2"/> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="3" android:background="#0000aa" android:gravity="center" android:text="3"/> </LinearLayout>
效果:
这样看来我们所需要的按比例又无法实现了,经过满天地google终于找到了解决方案,就是设置layout_width设置为"wrap_content"。配置及效果见下:
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="#aa0000" android:gravity="center" android:text="1111111111111111111111111111111111111111111"/> <TextView android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="2" android:background="#00aa00" android:gravity="center" android:text="2"/> <TextView android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="3" android:background="#0000aa" android:gravity="center" android:text="3"/> </LinearLayout>
效果:
这样终于达到我们的按比例显示的效果了,感觉很是奇怪,android开发框架的大佬们有时候设计确实有点匪夷所思。
二、LinearLayout内的控件的layout_width设置为"fill_parent",请看一下xml配置:
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#aa0000" android:gravity="center" android:text="1"/> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="2" android:background="#00aa00" android:gravity="center" android:text="2"/> </LinearLayout>
效果如下:
奇怪吧,整个宽度平分3块,第一个TextView占了两块,这样看来weight值越小的优先级越大。只有两个TextView似乎看出些道理,那么让我们看看三个是什么效果:
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#aa0000" android:gravity="center" android:text="1"/> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="2" android:background="#00aa00" android:gravity="center" android:text="2"/> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="3" android:background="#0000aa" android:gravity="center" android:text="3"/> </LinearLayout>
效果:
什么意思?第三个TextView丢掉了,很是奇怪,让我们再试一个,把weight分别改为2,3,4的看看效果:
这个效果让人很困惑,我一直想寻求出一个确切的比例公式,但是至今未找到。有哪位大神能搞定的话忘不吝赐教。
虽然这个android:layout_weight属性很怪异,但幸运的是我们达到了目标:
按比例显示LinearLayout内各个子控件,需设置android:layout_width="0dp",如果为竖直方向的设置android:layout_height="0dp"。在这种情况下某子个控件占用LinearLayout的比例为:本控件weight值 / LinearLayout内所有控件的weight值的和。
那个layout_height和layout_width的优先权高于layout_weight,layout_weight属性设置的参数是按照那个前者分配完了之后剩余的部分按比例分配,考虑weight分别改为2,3,4,之前的如果分配为fill_parent的话把,剩余的为1个parent_width (parent_width指的是屏幕宽度 )-3个parent_width=(-2个parent_width ),在按比例分配 :textview1说占空间=1个parent_width+2/9(-2个parent_width)=(5/9个parent_width)其他类似
帮人找个入门级别的书得负责任,自己先看一下有无缺陷,结果发现这个:(原文发在该书的论坛上,发现受众面太小了,无奈转到这个弃用很久的博客里,以后把心得慢慢发上来)
Google Android开发入门与实战,第二版,P104倒数第七行括弧内容:(layout_weight越小权重越大)。
SDK中的原话:Indicates how much of the extra space in the LinearLayout will be allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.
这是什么意思呢?主要是两个关键点:extra space和pro-rated among all views,额外的空间和大小比例
说到额外的空间,不得不提另外几个属性,假设LinearLayout的android:orientation="horizontal",即水平分布,在三个控件的存在情况下,其layout_weight属性值分别为122(单个控件在某些情况下会标警示黄线,请自己尝试):
第一种情况:每个控件的宽度属性都为android:layout_width="0dp",那么额外的空为(手机的宽度假设为X)X-0-0-0=X,那么根据sdk上所述:
第一个控件的宽度为 0+(1/(1+2+2))*X=X/5
第二个控件的宽度为 0+(2/(1+2+2))*X=2X/5
第三个控件的宽度为 0+(2/(1+2+2))*X=2X/5
第二种情况:每个控件的宽度属性都为android:layout_width="match_parent",那么额外的空间就是X-X-X-X=-2X
第一个控件的宽度为 X+(1/(1+2+2))*(-2X)=3X/5
第二个控件的宽度为 X+(2/(1+2+2))*(-2X)=X/5
第三个控件的宽度为 X+(2/(1+2+2))*(-2X)=X/5
第三种情况:其中第一个控件宽度属性为android:layout_width="match_parent",其他两个控件宽度为android:layout_width="0dp",额外的空间为X-X-0-0=0
第一个控件的宽度为 X+(1/(1+2+2))*0=X
第二个控件的宽度为 0+(2/(1+2+2))*0=0
第三个控件的宽度为 0+(2/(1+2+2))*0=0
第四种情况:第一个和第二个控件宽度属性为android:layout_width="match_parent",第三个控件宽度为android:layout_width="0dp",这个大家自己动手算算,结果很有趣。
最终结论:layout_weight的公式
控件的宽度(高度)=控件的width(height)值+(该控件的weight值/所有控件的weight的和)×额外的空间
额外的空间=手机的宽度(高度)-所有控件的宽度(高度)
有质疑的可以自己去写个代码尝试一下,希望都能踏踏实实的学习、工作,少点人云亦云,有自己的思想,网络上有关layout_weight的文章大部分都是错误的,希望本帖可以帮助一部分人。
这里拿宽度举例子控件设置完layout_weigth值后所得到的空间就是控件的layout_width值+(设置的weigth值/所有控件设置的weight的和)/额外空间(手机宽度-所有控件宽度------也就是一个parent减去你分配的layout_width值)
- layout_wigth详解
- 详解
- 详解
- 详解
- 详解
- &,&&,|,||详解
- 详解
- Scala详解--------基础知识详解
- Spring详解-----------事务详解
- github 详解详解
- InputFilter详解、TextWatcher详解
- Spring详解-----------事务详解
- 【词汇详解】事务详解
- Session 详解
- Session 详解
- sizeof详解
- 端口详解
- Meta详解~~~
- 根据手势移动布局
- linux 下修改 apache 启动的所属用户和组
- 查看java jdk安装路径和设置环境变量
- Exchange 2013的OWA页面出现503错误的解决办法
- javascript原生移动云编程11 - 如何调用手机图库中图片
- layout_wigth详解
- protocol buffers Scalar Value Types
- 使用物理引擎进行碰撞检测
- Json-lib 自定义序列化和反序列化
- 报表制作
- js中获取月份和季度的最后一天
- 统计代码行数
- 表 T006 和 T006_OIB 不匹配
- ZOJ1654