最近在写项目的时候经常要进行屏幕适配,很多时候都会遇到layout_weight属性,很多人也不太清楚这个属性是怎么去使用,包括之前我自己都有点用不好,经常会弄混淆,那么今天就来稍微整理下。
layout_weight属性的作用:主要是用来分配剩余空间的一个属性,可以设置它的权重。特别注意一点就是,这个属性在Linearlayout下设置才有效。
下面我们来看看以下几种情况:
第一种情况:我们将LinearLayout的布局设置为垂直排列,并且Button的高度都设置为wrap_content,权重分别为1,2,3。然后我们看看下面的效果图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
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="2"
android:text="button2" >
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="button3" >
</Button>
</LinearLayout>
从上图可以看出,是将整个屏幕的高度分成了6份,然后根据权重的大小来分配剩余的空间。button1是占1/6,button2是占1/3,button3是占1/2。
第二种情况:依然设置为LinearLayout的布局设置为垂直排列,但是我将Button的高度都设置为match_parent,各个Button所占的权重依然是1,2,3;接着看下面的效果图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2"
android:text="button2" >
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="3"
android:text="button3" >
</Button>
</LinearLayout>
咦,大家一看肯定很迷糊,这个Button3跑哪去了呢?当设置了layout_weight这个属性之后,那么这个View的高度就等于原来的高度加上剩余空间所占的比例。那么剩余空间怎么求呢?下面我来告诉大家,对于这里来说,求的就是剩余的高度:首先我们假设屏幕的高度是height,因为每个View都是设置的match_parent,所以每个View的高度都是height,那么这里有三个Button,也就是三个View,剩余的高度就是用屏幕的高度-3*view的高度,height-3*height=-2height,这样我们就把剩余的高度求出来了,下面我们分别算出每个Button的高度:button1 = height+(-2*height)*1/6 = 2/3height;button2的高度为:button2 = height + (-2*height)*1/3 = 1/3height;button3的高度为:button3 = height + (-2*height)*1/2 = 0;所以根据计算的结果,屏幕的高度分配就是2:1:0,所以button3就没有空间可以显示了。
第三种情况:我们将LinearLayout的布局设置为水平排列,并且Button的宽度都设置为wrap_content,权重分别为1,2。然后我们看看下面的效果图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
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="2"
android:text="button2" >
</Button>
</LinearLayout>
从上图可以看出,是将整个屏幕的高度分成了3份,然后根据权重的大小来分配剩余的空间。button1是占1/3,button2是占2/3。
第四种情况:依然设置为LinearLayout的布局设置为水平排列,但是我将Button的宽度都设置为match_parent,各个Button所占的权重依然是1,2,3;接着看下面的效果图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
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>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="button3" >
</Button>
</LinearLayout>
button3依然没有显示出来,这里就不分析了,就是把上面的高度改为宽度,然后就自己去算吧,原理都是一样的。
最后来稍稍总结一下吧:首先 layout_weight这个属性是在LinearLayout中设置才有效,当LinearLayout的方向为垂直排列的时候,我们layout_weight是针对高度来进行权重的分配;当LinearLayout的方向为水平排列的时候,我们layout_weight是针对宽度来进行权重的分配。
枫