<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="16dp" android:paddingRight="16dp" android:orientation="vertical" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/to" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/subject" /> <EditText android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="top" android:hint="@string/message" /> <Button android:layout_width="100dp" android:layout_height="wrap_content" android:layout_gravity="right" android:text="@string/send" /> </LinearLayout> <!-- android:orientation="vertical" 表明方向垂直--> <!-- android:layout_weight="1" 表明所佔權重(比例) message佔滿其餘元素剩餘的地方, 若是有多個android:layout_weight=1,則平分剩餘空間 android:gravity="top"表示從上面開始佔(寫了android:layout_weight的) android:layout_gravity="right" 整個組件的位置居右 -->
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="16dp" android:paddingRight="16dp" android:orientation="horizontal" > <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/to" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/subject" /> <EditText android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:gravity="top" android:hint="@string/message" /> <Button android:layout_width="100dp" android:layout_height="wrap_content" android:layout_gravity="right" android:text="@string/send" /> </LinearLayout>
LinearLayout
是一個視圖組,它全部的子視圖都在一個方向對齊,水平或者垂直。你能夠指定佈局的方向經過 android:orientation
屬性。html
LinearLayout
的全部子視圖排列都是一個靠着另外一個,所以垂直列表每行僅僅有一個子視圖,無論有多寬。水平列表只能有一行的高度(最高子視圖的高度加上邊距距離)。LinearLayout
指望子視圖之間都有margin,每一個子視圖都有gravity。java
線性佈局支持給個別的子視圖設定權重,經過android:layout_weight屬性。就一個視圖在屏幕上佔多大的空間而言,這個屬性給其設定了一個重要的值。一個大的權重值,容許它擴大到填充父視圖中的任何剩餘空間。子視圖能夠指定一個權重值,而後視圖組剩餘的其餘的空間將會分配給其聲明權重的子視圖。默認的權重是0.android
例如,若是有三個文本框,其中兩個聲明的權重爲1,另一個沒有權重,沒有權重第三個文本字段不會增長,只會佔用其內容所需的面積。其餘兩個一樣的會擴大以填補剩餘的空間,在三個文本域被測量後。若是第三個字段,而後給定的權重爲2(而不是0),那麼它如今的聲明比其餘的更重要,因此它獲得一半的總的剩餘空間,而前兩個平均分配剩餘的。ide