有關的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.。