1. 程式人生 > >安卓-API GUIDES--Linear Layout

安卓-API GUIDES--Linear Layout

LinearLayout是一個ViewGroup,它使得所有的子檢視在一個獨立的方向成一線,水平方向或垂直方向。你可以使用android:orientation屬性指明佈局的方向。


LinearLayout的所有子檢視被堆積,一個接一個,所有在一個垂直的列表每一行僅僅只有一個子檢視,不管這些檢視有多寬,一個水平的列表將只有一行高。一個LinearLayout考慮子檢視之間的邊距和每個子檢視的比重。

一 Layout Weight

LinearLayout也支援使用android:layout_weight屬性為每個子檢視設定一個比重。這個屬性設定一個重要的值去說明這個檢視應該在螢幕上佔有多少空間。一個大的重量值執行它擴充套件填充剩餘的空間。子檢視可以指定一個重量值。 預設是0.

例如,如果你有三個文字域和他們中的兩個宣告重量是1,其他沒有給定比重,第三個字元域不會增長,將僅僅佔有它自己的區域。另外兩個可以擴充套件和填充剩餘的空間。

三 例項

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns: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"><EditTextandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:hint="@string/to"/><EditTextandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:hint="@string/subject"/><EditTextandroid:layout_width="fill_parent"
android:layout_height="0dp"android:layout_weight="1"android:gravity="top"android:hint="@string/message"/><Buttonandroid:layout_width="100dp"android:layout_height="wrap_content"android:layout_gravity="right"android:text="@string/send"/></LinearLayout>

下面我們演示下這個例子


增加android:layout_gravity="right"