Android layout_weight的全面剖析
部分文章内容来自:http://www.cnblogs.com/JohnTsai/p/4077183.html
对layout_weight的分析分为四个部分
首先我们我们看看Android 官方文档中的解释:
从上面的说明中可以看出:
1、layout_weight属性只能够使用在LinearLayout布局当中
2、layout_weight是用来指定一个组件到底如何占据一个LinearLayout布局中相应部分的剩余空间,
当我们指定一个组件的layout_weight= 0 (这也是Android默认的值)时,那么这个组件将会按照layout_width或者layout_height的值来进行显示,而不会被拉伸,
如果一个组件的layout_weight的值大于0 , 那么这个组件将会和其他的那些layout_weight值也大于0 的组件一起按照比例来填充剩余的空间
解释的非常清楚,那么我们来看看下面这几种情况:
【1】组件并没有显式的指定layout_weight属性值
我们在设定这样的一个布局文件
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal"> 6 7 <TextView 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:background="#00FFEE" 11 android:text="@string/tx1 /> 12 13 <TextView 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:background="#EEFF00" 17 android:text="@string/tx2"/> 18 19 </LinearLayout>
在这种情况下,组件的layout_weight的默认值为 0 ,也就是说,并不对两个TextView进行拉伸,完全按照layout_width = wrap_content来显示
【2】设置两个TextView的Layout_weight的值都为1。
在上面的xml文件中,给每个TextView增加一个"android:layout_weight=1"属性。这时首先程序按照两个layout_width=wrap_content属性,为两个TextView进行第一次的空间分配,也就说第一次两个组件都是wrap_content,这时候发现对应的空间中有剩余空间,那么就要根据组件的layout_weight值,对组件按照比例惊醒拉伸,将剩余空间填满所以最终的显示为
TextView1,TextView2的layout_weight值分别设置为2和1,1和2,看运行的效果:
2和1:
1和2:
【3】组件的layout_width或者layout_height 都设为 0dp ,之后再指定layout_weight值
这样的话也非常容易明白,当我们将layout_width和layout_height的值设为 0dp 时, 意思也非常的明白:不用为组件进行第一的空间分配,而是直接根据layout_weight的值进行空间的比例分配
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 android:orientation="horizontal" > 6 7 <TextView 8 android:layout_width="0dp" <!--将layout_width值设置为0dp以避开第一步的空间分配--> 9 android:layout_height="wrap_content" 10 android:background="#f00" 11 android:layout_weight="1" <!--LinearLayout将会按此值分配空间--> 12 android:text="TextView1" /> 13 <TextView 14 android:layout_width="0dp" 15 android:layout_height="wrap_content" 16 android:background="#0f0" 17 android:layout_weight="1" 18 android:text="ThisIsTextView2" 19 /> 20 </LinearLayout>
【4】当组件的layout_width或者layout_height同时为math_parent或者同时为wrap_content , 并同时为之指定layout_weight值
在layout_width设置为match_parent的时候,layout_weight越小,优先级越高,即layout_weight越小的组件要优先的尽可能大,但这个大是有限度的,即match_parent
在layout_width设置为wrap_content的时候,layout_weight越小,优先级越高,即layout_weight越小的组件要优先的尽可能小,但这个小是有限度的,即wrap_content
layout_height和layout_width一样
实例1
<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"> <TextView android:layout_width= "wrap_content" android:layout_height="wrap_content" android:layout_weight="2" android:background="#00FFEE" android:text="@string/hello_world" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="#EEFF00" android:text="@string/hello_world"/> </LinearLayout>

实例2
<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"> <TextView android:layout_width= "wrap_content" android:layout_height="wrap_content" android:layout_weight="2000" android:background="#00FFEE" android:text="@string/hello_world" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="#EEFF00" android:text="@string/hello_world"/> </LinearLayout>

实例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"> <TextView android:layout_width= "match_parent" android:layout_height="wrap_content" android:layout_weight="2" android:background="#00FFEE" android:text="@string/hello_world" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#EEFF00" android:text="@string/hello_world"/> </LinearLayout>

实例4
<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"> <TextView android:layout_width= "match_parent" android:layout_height="wrap_content" android:layout_weight="200" android:background="#00FFEE" android:text="@string/hello_world" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#EEFF00" android:text="@string/hello_world"/> </LinearLayout>

注意左边的TextView被挤成了一条

浙公网安备 33010602011771号