之前android的学习或者是工作中一直不是太懂权重的含义,可能只是学习时似懂非懂的一句口诀当android:layout_width="match_parent时,layout_weight的值越小,他的权重越大,优先级越高。但是这是为什么呢?这里我来总结一下:一,和layout_width有关系时:1,当android:layout_width="wrap_content"时,layout_weight的值越小,他的权重越小,优先级越低

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="30dp"
    android:orientation="horizontal" >

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

以下便是效果图:

android设置界面权重 android权重如何计算_android


但是呢!这里是有问题的要是第一个textview里面的内容长一些的话,于是我们将以下代码进行修改,修改后如下

<TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#aa0000"
        android:gravity="center"
        android:text="11111111111111111111111111111111" />


然后效果图如下:看出权重已经失效

android设置界面权重 android权重如何计算_权重_02

所以尽量不要包裹内容

2,当android:layout_width="0dp"时,layout_weight的值越小,他的权重越小,优先级越低

这种情况和第一种其实几乎一样  唯一的区别在于

android设置界面权重 android权重如何计算_android_03




所以到目前为止推荐使用第二种

3,当android:layout_width="match_parent时,layout_weight的值越小,他的权重越大,优先级越高

同理代码几乎一样,只是android:layout_width="match_parent这是唯一的区别,先贴上代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="30dp"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#aa0000"
        android:gravity="center"
        android:text="11111111111111111111111111111111" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:background="#00aa00"
        android:gravity="center"
        android:text="1" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="3"
        android:background="#0000aa"
        android:gravity="center"
        android:text="1" />

</LinearLayout>

然后看下效果图:

android设置界面权重 android权重如何计算_android_04


看到效果图这时候我们就有些不懂了,为什么呢?

控件分配规则:最后控件宽度 = 控件本身的宽度  +  权重比例分配宽度

主要原因是因为:

adroid:layout_width = "match_parent"系统给三个textview分配他们所要的宽度match_parent,也就是每一个都填满他们的父控件,这里就是屏幕的宽度

那么这个时候的剩余空间 = 1个match_parent - 3个match_parent = -2个match_parent(这里match_parent就是屏幕的宽度)

那么第一个Textview所占的宽度 =  match_parent   +  它所占剩余空间权重比例1/6  *  剩余空间大小(-2match_parent) = 2/3match_parent   

那么第二个Textview所占的宽度 =  match_parent   +  它所占剩余空间权重比例2/6  *  剩余空间大小(-2match_parent) = 1/3match_parent   

那么第三个Textview所占的宽度 =  match_parent   +  它所占剩余空间权重比例3/6  *  剩余空间大小(-2match_parent) = 0match_parent

所以第三个控件就不会显示出来了

二,第二种情况android:layout_weight = "0"

这种情况有什么用

android设置界面权重 android权重如何计算_控件_05


贴上代码:

<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="vertical"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >
    </ListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0" >

        <Button
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="哈哈" />
    </LinearLayout>

</LinearLayout>

ListView占一份,button占0,这种方式很有用,能够确保listview最下方留下button的控件,不管listview有多长,用户都不需要拉到最后才看到button,确保button出现在屏幕的最下方。