在android的布局文件中有一个属性:Layout_weight,直译为布局的重量级,即占布局中的重量比重,比如:
<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="9"/> <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"/>
这里layout02的layout_weight值为1,在android计算时,数字小的优先级高,这样就是layout02在申请占屏幕空间的时候,因为layout_width=fill_parent,填充父控件空间,所以就是占有的空间尽可能的大,两份空间,一份9/10,一份1/10,Layout02据有优先级,所以会占用9/10的空间。
如果android:layout_width="wrap_content"时候,情况就不一样了。
<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="9" /> <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
layout02将占据总量1+9的1份空间,这是怎么回事呢?
这是因为当layout_width=wrap_content时,控件的大小是适应内容的宽度,意思是尽可能的小,所以layout02在挑选空间的时候,一份1/10,一份9/10,只有挑选1/10的空间了。