彻底搞明白layout_weight

来源:互联网 发布:亚瑟士k24 知乎 编辑:程序博客网 时间:2024/05/17 07:41

不得解惑的几种说法

之前也一直用到layout_weight,被几本书上和网络上的参考说法,搞得有点晕~~

说法一:系统把所有控件指定的layout_weight值相加,得到一个总值,然后每个控件所占大小的比例就是用该控件的layout_weight值除以刚才算出的总值。

说法二:layout_weight属性是用于设置组件的重要程度。重要程度越高,值越低。

有没有觉得这两种说法自相矛盾

几个例子

例一

先来看下官网的例子
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingLeft="16dp"    android:paddingRight="16dp"    android:orientation="vertical" >    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="to" />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="subject" />    <EditText        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:gravity="top"        android:hint="message" />    <Button        android:layout_width="100dp"        android:layout_height="wrap_content"        android:layout_gravity="right"        android:text="send" /></LinearLayout>
运行结果如下:我们结合官网说法分析一下,在这里面只有第三个EditText设置了权重,其余没设的,默认值为0。那我们通过这个例子可以看到,第二种说法是错误的。官方说,layout_weight的值相当于给控件设置了一个重要程度,这个重要程度决定了它在界面上能够占用的空间。很明显,这个例子说明layout_weight的值越高,占用的空间越多。(有一定的前提,我们接着往下看)其它的控件都是占用了hegiht分配的大小,而message占用的是其余的所有空间。

例二

再看一个布局的例子
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    tools:context=".MainActivity">       <Button           android:layout_width="match_parent"           android:layout_height="wrap_content"           android:text="1"           android:layout_weight="2"/>       <Button           android:layout_width="match_parent"           android:layout_height="wrap_content"           android:text="2"           android:layout_weight="1"/>       <Button           android:layout_width="match_parent"           android:layout_height="wrap_content"           android:text="3"           android:layout_weight="2"/>   </LinearLayout>
运行结果如下:我们可以看到3个Button的权重值分别是2,1,2,按照下面的运行结果,貌似第二种说法是对的,权重越大,占用的空间越小。先别着急,我们再看一个例子,感觉离真相越来越近了~~

还是上面的布局,我将三个Button的权重修改一下。分别修该为1,2,3。如下所示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    tools:context=".MainActivity">       <Button           android:layout_width="match_parent"           android:layout_height="wrap_content"           android:text="1"           android:layout_weight="1"/>       <Button           android:layout_width="match_parent"           android:layout_height="wrap_content"           android:text="2"           android:layout_weight="2"/>       <Button           android:layout_width="match_parent"           android:layout_height="wrap_content"           android:text="3"           android:layout_weight="3"/>   </LinearLayout>
运行结果如下:哎~Button3直接没有了。这是什么原因呢?Button1和Button2将横向的布局平分了。这下子感觉说法1和说法2都不对了。


我们结合这三个例子,说明一下,权重真正的计算方式。
首先,官方的说明肯定是没错的,官方是这么说的,
 This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen.  A larger weight value allows it to expand to fill any remaining space in the parent view. 
意思是说,weight属性相当于给view分配了一个重要的属性值,这个值与它在屏幕上占用的空间有关。那有什么关系呢?较大的weight值允许扩长填充父View中剩余的空间。

既然是剩余的空间,所以我们首先不要忽略了其他控件表示长宽的属性值设置。例如layout_wegith和layout_height。

我们先看例1,因为LinearLayout设置的是垂直方向的,EditTexts以及Button的高度分别是,wrap_content,wrap_content,0(权重为1),wrap_content。第三个的EditText的长度=总控件-其余控件正常的长度。
那我们可以这样想,首先我们按照height,weight设置的值布置控件,然后其余的空间给设置了权重的组件。我们更改一下其余组件的高度,看看有没有可能设置权重的占用的空间比没设的要小。将height的值除了第三个EditText,全部改成150dp。运行的结果如下:这证明我们所假设的结果是正确的。

那结合着上面的推论,我们来解释一下例2和例3。

例2中三个Button权重的值分别是2,1,2。LinearLayout设置的是水平方向的,先看一下三个Button的width值设置的全是match_parent。如果在没有设置权重的情况下,三个Button只会显示一个Button1,其他的都被覆盖。但是设置了权重就不能这么想了,我们先假设他们每个Button都占用了一个match_parent的空间。也就是先按3个Button给定的width=match_parent的值计算剩余的空间。
剩余的空间=1个match_parent的空间—3个match_parent的空间 = -2个match_parent的空间
weight的总值=2+1+2=5;
那 Button1占用空间=1个match_parent+(-2个match_parent空间)*2/5 = 1/5个match_parent空间
    Button2占用空间=1个match_parent+(-2个match_parent空间)*1/5 = 3/5个match_parent空间
    Button3占用空间=1个match_parent+(-2个match_parent空间)*2/5 = 1/5个match_parent空间
这样计算的结果与例2运行的结果一致。

例3中权重的值为1,2,3,其余的没有改变
Button1占用空间=1个match_parent+(-2个match_parent空间)*1/6= 2/3个match_parent空间
Button2占用空间=1个match_parent+(-2个match_parent空间)*2/6 = 1/3个match_parent空间
Button3占用空间=1个match_parent+(-2个match_parent空间)*3/6 = 0个match_parent空间
我们发现Button3计算的结果果然为0,与运行结果一致。

我们在计算一下例1吧。wrap_content、wrap_content、0、wrap_content。
剩余的空间=match_parent — 3*wrap_content;
权重的总值=1
那第三个EditText空间 = 剩余空间。



参考链接:http://developer.android.com/guide/topics/ui/layout/linear.html
                  http://mobile.51cto.com/abased-375428.htm










0 0
原创粉丝点击