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值)

 

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 在家啃老三年了怎么办 新时代卫计工作怎么办 被公司辞退不发工资怎么办 被公司辞退后不发工资怎么办 领导分配的工作太多怎么办 领导故意不给活怎么办 户口迁移后医疗社保怎么办 有了c证考b证怎么办 顶替姐姐上班已到退休年龄怎么办 年龄过60岁厂里拖欠工资怎么办 领导找人顶替我怎么办 宁夏超生了没钱交罚款怎么办? 户口年龄上大了怎么办 孩子年龄报小了怎么办 招工档案年龄有涂改怎么办 退伍军被别人顶替上班怎么办 二孩政策前生的怎么办 孩子晕车怎么办最有效方法得当 事业单位编外人员改革工伤怎么办 工伤仲裁后法院一审判决后怎么办 我媳妇删了我该怎么办 老婆离家出走不照顾小孩怎么办 车停在4s店损坏怎么办 车辆年检贴丢了怎么办 卖衣服别人嫌贵怎么办 武汉铁路医保卡丢了怎么办 高铁列车员年龄大了怎么办 尚客优酒店会员怎么办 钢铁雄心4人力不足怎么办 未经车主同意私自将车卖了怎么办 剧本给几个制片人看过怎么办 没有产品经理ui设计师怎么办 老板请朋友吃饭司机应该怎么办 被化妆学校坑了怎么办 快车约得太远怎么办 工资好低2000多怎么办 苹果手机不能下载软件怎么办 苹果手机下不了软件怎么办 苹果6s下不了软件怎么办 苹果6下不了软件怎么办 ipad更新系统卡住了怎么办