android:layout_weight详细分析介绍:

布局文件是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button2"
/>
</LinearLayout>

出现的布局是:button1占了2/3,button2占了1/3。

但是如果将布局文件中的button的属性android:layout_width="fill_parent"改为android:layout_width="wrap_content"那么出现的结果为:button1占了1/3,button2占了2/3。

出现这样的结局是什么意思呢?下面是详细解释:

linearLayout中包含有weight的child时,linearLayout会measure两次:
设屏幕宽度为X
第一次:button1的measuredWidth为X,button2也为X (因为用了weight,所以linearLayout每次measure child时不考虑前一个已经占用的大小),total_width为2X
第二次:计算delta=x-total_width=-x,然后会将button1的宽度设为
x+delta*1/3=0.66x, button2的宽度为 x+delta*2/3=0.33x

那我现在对这句话重新概括一下:“因为设置了button1的权重最小,所以它占用的布局优先级就越高”,也就是说layout_weight越小,布局优先级越高。也许在Android里面布局并没有优先级之说,我这里只是为了说明问题,自己定义的,所以朋友们不要拍砖。
      那首先分析一下当layout_width属性设置为fill_parent的时候,即充满父布局,当然意思是这个控件要根据weight的设置尽可能的大,因此,依上例而论,button1的weight设为1,button2的weight设置为2.即button1的优先级最高,因此,要填充父布局就要button1先来填充,尽可能的大,那这个尽可能又是多少呢,这就要综合layout里其它控件的weight值了,然后做一下运算,button1占据2/3,button2占据1/3.你也可以把button2设置为一个非常大的数,比如2000,此时在Graphical Layout模式下可以看到button1填充满了整个宽度,而看不到button2的影子,事实上button2还是存在的,你把鼠标指向button1的后面就可以看到一个长长的竖条,那个就是button2,已经非常非常小了。因此,在layout_width设置为fill_parent的时候,weight所代表的是你的控件要优先尽可能的大。

接着是当layout_weight设置为wrap_content的时候,即适应内容的宽度,意思是这个控件要尽可能的小,只要能把内容显示出来就可以了,同样的,如果把button1和button2的layout_weight设置为wrap_content后,button1的weight为1,button2的weight为2.那么button1要优先尽可能的小,而button2也要尽可能的小,只是优先级不一样,因为设置了weight,所以这两个控件总的宽度要填满父布局的宽度,所以就又要计算每个控件所占据的大小,此时,button1的优先级较高,共有两份,一份1/3,一份2/3,button1要尽可能的小,那button1当然要选1/3,因此,我们看到的效果反而是button2占据的较大。这里要说的是如果把权值同样做如下设置:button1为1,button2为2000,那button1是不是就要占据1/2000的空间呢?这么理解就错了,刚才说了,要尽可能的小,但这个小是有一个限度的,那就是wrap_content,就是还要是内容完完整整的显示出来,同样的,尽可能的大也是有一个限度的,那就是父布局的宽度。因此,在layout_width设置为wrap_content的时候,weight所代表的是你的控件要优先尽可能的小。

所以,要对weight做了解,要深深的理解下面两句话:
在layout_width设置为fill_parent的时候,layout_weight所代表的是你的控件要优先尽可能的大,但这个大是有限度的,即fill_parent.
在layout_width设置为wrap_content的时候,layout_weight所代表的是你的控件要优先尽可能的小,但这个小是有限度的,即wrap_content.
layout_height 同 layout_width.

实际上上面做的解释主要是对fill_parent和wrap_content做的解释,我们可以参考下面的2篇博文知道,上面的解释是存在一定的问题的。

一般来说,我们使用layout_weight,我们可能会使用在各个控件或者布局(像LinearLayout),应用最广泛的就是DV6300-T的主界面的布局,全部用的是layout_weight来控制的,这样可以更好的适应不同的分辨率。

所以我们经常用layout_weight来设置布局,一般是设置不同的LinearLayout为不同的layout_weight来完成基本布局,还可以利用LinearLayout来占据空间都是没有任何显示,这样的就是完全实现比重布局的LinearLayout,最后在各个LinearLayout里面又实现多个控件的layout_weight控制,同样也可以使用只占控件无显示的控件来达到分配布局的效果,DV6300-T就是明显例子。

还需要注意的就是LinearLayout的orientation如果没有设置,那么默认的就是horizontal水平方向。

按比例显示LinearLayout内各个子控件,需设置android:layout_width="0dp",

如果为竖直方向的设置android:layout_height="0dp"。

在这种情况下某子个控件占用LinearLayout的比例为:本控件weight值 / LinearLayout内所有控件的weight值的和。

一般我们控制比重都是设置对应方向的高或宽为0dp来实现我们想的比重控制,而很少用fill_parent或wrap_content,不过我们来理解这2种设置的区别。

还有一句话需要理解就是: 权重(layout_weight)最小,所以它占用的布局优先级就越高。

还需要注意的是保存一致性控制

如果我们要控制显示比重,那么我们一般要求各个等级的元素的控制方式必须一致,比如多个button控件处于同一等级来控制比重布局,那么如果设置的是android:layout_width="0dp"这样的方式来控制,就必须把所有的等级控件都用android:layout_width="0dp"来控制,而不能有的用android:layout_width="0dp",而还有的用android:layout_width=" fill_parent ",这样就会出现混乱了。

下面还有一个例子:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1">

<Button

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="2"

android:background="#00AAC0"

/>

<Button

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="2"

android:background="#CCdd00"

/>

<Button

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="#0000FF"

/>

</LinearLayout>

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="2">

<Button

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="2"

android:background="#FFAA00"

/>

<Button

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="2"

android:background="#CCdd00"

/>

<Button

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="#0000FF"

/>

</LinearLayout>

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="2">

<Button

android:layout_width="0dp"

android:layout_height="fill_parent"

android:layout_weight="2"

android:background="#00FF00"

/>

<Button

android:layout_width="0dp"

android:layout_height="fill_parent"

android:layout_weight="2"

android:background="#CCdd00"

/>

<Button

android:layout_width="0dp"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="#cc00FF"

/>

</LinearLayout>

</LinearLayout>

看看最后的效果图:

【转】安卓布局:layout_weight的理解的更多相关文章

  1. 安卓布局修改基础常识篇之TextView属性

    [天使]安卓布局修改基础常识篇之TextView属性 在修改布局xml文件时需要熟练掌握一些属性,以下是TextView也就是文本的属性:android:autoLink 是否自动链接网址或邮箱地址: ...

  2. 【转】android中layout_weight的理解

    android Layout_weight的理解 SDK中的解释: Indicates how much of the extra space in the LinearLayout will be ...

  3. RelativeLayout相对布局 安卓布局技巧

    http://blog.csdn.net/nieweiking/article/details/38417317 RelativeLayout相对布局 相对布局 RelativeLayout 允许子元 ...

  4. rem布局原理深度理解(以及em/vw/vh)

    一.前言 我们h5项目终端适配采用的是淘宝那套<Flexible实现手淘H5页面的终端适配>方案.主要原理是rem布局.最近和别人谈弹性布局原理,发现虽然已经使用了那套方案很久,但是自己对 ...

  5. 安卓开发_深入理解Handler消息传递机制

    一.概述 因为子线程的run()方法无法修改UI线程(主线程)的UI界面,所以Android引入了Handler消息传递机制,实现在新创建的线程中操作UI界面 二.消息类(Message) 消息类是存 ...

  6. Flexbox布局模式的理解

    个人博客地址:  雨中的鱼-前端知识分享   http://www.showhtml5.cc    分享干货,有兴趣的人可以一起来分享前端知识  加Q群:440279380   Flexbox,一种C ...

  7. Android中设置半个屏幕大小且居中的button布局 (layout_weight属性)

            先看例如以下布局 : 

  8. 【安卓进阶】Scroller理解与应用

    项目中有个需求,就是在RecyclerView的item中进行侧滑,一开始同事推荐了一个开源库,使用起来确实也方便好用,直接在布局作为父布局即可实现侧滑. 自己也非常好奇这个开源库到底用了什么API能 ...

  9. 安卓开发_深入理解Content Provider

    一.Content Provider概述 Content Provider用于保存和获取数据,并使其对所有应用程序可见,这是不同应用程序之间共享数据的唯一方式,因为在Android中没有提供所有应用可 ...

随机推荐

  1. 转:浏览器与WEB服务器工作过程举例

    用户通过“浏览器”访问因特网上的WEB服务器,浏览器和服务器之间的信息交换使用超文本传输协议(HTTP--HyperText Transfer Protocol). 例:用户访问东南大学主页 Http ...

  2. VC与ADO数据库操作

    VC与ADO数据库操作 学研部的同志们,大家好! 想开一次学习会,实习时间冲突了,只好把文档发给大家看了.重点推荐李振龙的BMP读图教程! 尤其是大三GIS班的同志,注意了,可能实习用得上的! 一.A ...

  3. Tomcat数据源

    Tomcat数据源原理,配置,使用 在程序中,使用数据源是可以提升操作性能的,这种性能 的提升依靠运行的操作原理. 传统的JDBC操作: 1. 加载数据库驱动,通过CLASSPATH配置 2. 通过D ...

  4. constraint 摘自群主大大

    CONSTRAINT 子句 限制和索引相似,虽然限制也能被用于建立和另一个表的关联. 用 ALTER TABLE 和 CREATE TABLE 语句中的 CONSTRAINT 子句来建立或删除条件.C ...

  5. MyEclipse报错 Building workspace has encountered a problem Errors occurred during the build 的2种解决方法

    1: Building workspace has encountered a problem Errors occurred during the build 如果报错这个 那么有可能是jar包,报 ...

  6. css一些特别效果设定

    在CSS中,BOX的Padding属性的数值赋予顺序为 padding:10px; 四个内边距都是10pxpadding:5px 10px; 上下5px 左右10pxpadding:5px 10px ...

  7. SDAU课程练习--problemG(1006)

    题目描述 Problem Description The highest building in our city has only one elevator. A request list is m ...

  8. Eclipse开发Maven项目提示:程序包org.junit不存在解决方案

    原因: 个人考虑产生此错误的原因是因为Eclipse中对于测试和开发的鉴定不明?Intellij中没有错误,因为Intellij对项目的管理就是同Maven结构的. 解决方案: 原来的junit的sc ...

  9. oracle 主键自动增长

    oracle 主键自动增长 2009-12-11 16:07:00|  分类: 数据库资料|字号 订阅     这几天搞Oracle,想让表的主键实现自动增长,查网络实现如下: create tabl ...

  10. (中等) HDU 5293 Tree chain problem,树链剖分+树形DP。

    Problem Description   Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.There are ...