天天看点

Day6.android layout_weight的用法?

       weight代表权重,也就意味着使用layout_weight会根据控件设置的权重分配大小,它的计算公式为:

# 实际尺寸: 子控件最终展示出来的尺寸
# 当前尺寸: 子控件刚被放到布局中, 系统还没有解析其weight属性时的尺寸
# weight比例: 子控件在所有有`layout_weight`属性的控件中所占的比例
#             (例: 三个子控件, 第一个比重为1, 第二个为2, 第三个为2,
#              那么第一个所占比例为1/5, 第二个为2/5, 第三个为2/5)
# 剩余空间: 子控件刚被放到父控件中, 系统还没有解析其weight属性时父控件还有多少剩余空间

剩余空间 = 父控件尺寸 - 子控件1尺寸 - 子控件2尺寸 - 子控件3尺寸...
实际尺寸 = 当前尺寸 + weight比例 * 剩余空间尺寸
           

layout_weight

属性很容易预测最终效果, 它们会按照自己的

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:gravity="center"
    android:orientation="horizontal">

    <Button
        android:id="@+id/begin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button1" /> 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button2" />

</LinearLayout>
           

        布局很简单,一个LinearLayout中包含了两个按钮,宽度都为wrap_content,layout_weight一个是1,一个是2,效果如下:

Day6.android layout_weight的用法?

            这个很容易理解,button1权重是1, button2权重是2,假设屏幕宽度为W,那么:

                  button1的宽度就是:  1/(1+2)*W,即为屏幕宽度的1/3; 

                  button2的宽度就是:  2/(1+2)*W,即为屏幕宽度的2/3;

            那button本身的宽度有什么作用呢?我们把两个按钮的宽度都设置为match_parent时,看看运行效果:

Day6.android layout_weight的用法?

                  结果居然反过来了!button1占据了屏幕宽度的2/3,button2占据了1/3!!!这是为什么呢?

                  额外控件尺寸!                                   

                  button1和button2都设置了match_parent,两个控件需要的尺寸为2*W,那么剩余空间尺寸为W-2*W=-W,由此:

                  button1的weight为1,那么它的实际尺寸为: W+ 1/(1+2)*-W= 2/3*W;

                  button2的weight为2,那么它的实际尺寸为: W+ 2/(1+2)*-W= 1/3*W;

                  所以才会出现上面的结果。要理解layout_weight分配的是额外空间尺寸,而不是屏幕尺寸!!

                  再加一个控件button3,使得它们的layout_weight为1:2:3:

<?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:gravity="center"
    android:orientation="horizontal">

    <Button
        android:id="@+id/begin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button1" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Button2" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="Button3" /> 
</LinearLayout>
           

               此时观察运行效果,会发现button3虽然占据的权重最大,但它居然都没有在屏幕上显示:

Day6.android layout_weight的用法?

               这是为什么呢,我们可以按照上面的计算方式,再计算下每个控件的实际尺寸:

                button1、button2、button3都设置了match_parent,三个控件需要的尺寸为3*W,那么剩余空间尺寸为W-3*W=-2*W,由此:

                  button1的weight为1,那么它的实际尺寸为: W+ 1/(1+2+3)*-2*W= 2/3*W;

                  button2的weight为2,那么它的实际尺寸为: W+ 2/(1+2+3)-2*W= 1/3*W;

                  button3的weight为3,那么它的实际尺寸为: W+ 3/(1+2+3)-2*W= 0;

                 button3的实际大小为0,当然不显示啦~~                

               接着修改,把button1的宽度设置为match_parent,button2的宽度设置为wrap_content,此时的效果为:

Day6.android layout_weight的用法?

                   按理说,button1设置了match_parent,它应该占满整个屏幕宽度,这样button2就不应该显示,可实际上button2却显示出来了,这是为什么呢?我一直没想明白,查了下,网上说wrap_content是包裹内容的最小尺寸,而layout_weight设置的是尽可能的显示,并不是不显示,wrap_cotent是有一个最小值的,但是这个最小值是怎么计算的呢?看了下View的onMeasure()方法,也并没有发现有默认值/最小值一说,费解啊。。。。

                   哪位大佬帮我解释下.....