彻底搞明白layout_weight

不得解惑的几种说法

之前也一直用到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://mobile.51cto.com/abased-375428.htm










  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: `layout_weight` 是在 Android 中用于布局的一个属性,它用于指定一个视图在布局中所占用的权重。布局中每个视图都有一个默认的权重为 `0`,当设置了 `layout_weight` 属性后,系统会根据视图的权重和所在容器的剩余空间来计算视图的实际大小。一个视图的实际大小会先根据自身的宽度或高度计算出来,然后再根据它的权重和其他视图的权重来分配剩余的空间。因此,通过设置 `layout_weight` 属性,可以实现在布局中更灵活的视图排列和分配。 ### 回答2: layout_weightAndroid布局中的一个属性,用于控制视图在父布局中的相对权重。 在LinearLayout中,父布局必须为水平或垂直方向,子视图的layout_weight属性决定它们在父布局中所占的比例。例如,如果有两个子视图,一个layout_weight为1,另一个为2,那么第一个子视图将占据父布局的1/3,而第二个子视图将占据2/3。 在RelativeLayout中,layout_weight属性无效,这是因为RelativeLayout是根据视图之间的关系来布局的。不过,可以结合使用其他属性来达到类似的效果。 layout_weight属性常用于构建灵活的布局,例如等分屏幕的两个或多个部分,或者按比例调整视图的大小。通过合理设置layout_weight,可以实现适应不同屏幕尺寸和方向的布局。 需要注意的是,更高的layout_weight并不一定意味着更大的视图,而是表示相对权重的增加。要实现适应不同分辨率的布局,还需要考虑使用dp和match_parent等属性。 综上所述,layout_weight是一种用于控制视图在父布局中相对权重的属性,可以帮助实现灵活的布局。了解并熟练使用该属性,可以更好地适配不同屏幕和实现更好的用户界面体验。 ### 回答3: layout_weightandroid中用于布局控制的一个属性。该属性只能在LinearLayout中使用。 layout_weight用于控制View在父容器中的分配空间比例。在LinearLayout中,如果某个方向的width或height设置为0dp(match_parent也可以),并且设置了layout_weight属性,那么这个View将根据layout_weight的值来分配父容器中剩余的空间。 假设有一个水平方向的LinearLayout,其中包含了两个子View:View1和View2。如果View1的layout_weight设置为1,View2的layout_weight设置为2,那么View1将会占据1/3的水平空间,而View2将占据2/3的水平空间。 另外,如果在设置了layout_weight的View中,将width或height设置为0dp,它将会以所占比例的形式拉伸或压缩来填充剩余空间。例如,如果View1的layout_weight设置为1,width设置为0dp,而View2的layout_weight设置为2,width设置为wrap_content,那么View1将会占据1/3的水平空间,并且会被拉伸或压缩以填充剩余的空间,而View2将会根据内容自动调整宽度。 总结来说,layout_weight属性允许我们在LinearLayout中根据所设置的权重值来分配剩余的空间,从而实现不同比例的布局效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值