1. 程式人生 > >Android開發之線性佈局(LinearLayout)

Android開發之線性佈局(LinearLayout)

老的Api(2.2或者2.3的)有五種佈局:

LinearLayout線性佈局,Relative Layout相對佈局,AbsoluteLayout絕對位置佈局,FrameLayout幀佈局,TableLayout表格佈局

而在最新的Api中只有兩種佈局:LinearLayout線性佈局,Relative Layout相對佈局

下面分析一下LinearLayout線性佈局:

所有的孩子只有一個方向vertically或者horizontally(用android:orientation="vertical"設定),如果想產生換行的效果就要用佈局巢狀,由於Android要解析XML,為了提高效能,一般不要包裹三層。

如下,用線性佈局實現簡單登入介面  

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

     <!-- 
     	android:layout_width="match_parent"
     	這裡要用內容包裹,如果用match_parent就會把 下一行的內容遮蓋住
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        	
         -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="使用者名稱:"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" >

            <requestFocus />
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密碼:"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" >

            <requestFocus />
        </EditText>
    </LinearLayout>
</LinearLayout>