有关的layout_weight代码: java
android
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > spa
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="one"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:text="two"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="three"/> </LinearLayout> code
分析:layout_weight属性,Android系统先按照你设置的3个EditText高度Layout_height值wrap_content,给你分配好他们3个的高度,而后会把剩下来的屏幕空间所有赋给EditText2,由于只有他的权重值是1,这也是为何EditText2占了那么大的一块空间。 xml
将上面的代码copy到 layout.xml 就能获得以下图效果: three
有了以上观点,咱们再来看看如下代码: utf-8
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" // 这里表示水平方向排列。 android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ff0000" android:layout_weight="1.0" android:text="one"/> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#dfdfdf" android:layout_weight="2.0" android:text="two"/> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ddaacc" android:layout_weight="3.0" android:text="three"/> </LinearLayout>
分析:当“orientation=horizontal"根据edittext 控件的layou_width,和 layout_weight 属性。很明显是将剩下的空间按1:2:3分别分配给EditText控件。 get
效果图以下 : it
而当layout_width=“fill_parent”时,若是分别给三个TextView设置他们的Layout_weight为1、2、2的话,就会出现下面的效果:3:1:1 io
分析: 系统先按照layout_width="fill_parent"给3个EditText 分配 "parentWidth"大小的空间(真空屏幕宽度)。那么 这里的剩下的空间是多少?????? “剩下空间”= “1 *parentWidth”- "3*parentWidth"= -2*parentWidth. 那么 “one真正的空间”= 1*parentWidth-(1/5)*2*parentWidth=3/5*parentWidth..同理咱们能够获得two,three 的都为真正空间为1/5*parentWidth...因此能够效果图为3:1:1.。