1. 程式人生 > >Android線性佈局與表格佈局

Android線性佈局與表格佈局

UI元件:佈局管理器

以ViewGroup為基類派生的佈局管理器

為了更好的管理Android應用的使用者介面裡的各元件,Android提供了佈局管理器。通過佈局管理器,Android應用的圖形使用者介面具有良好的平臺無關性。
為了讓這個元件在不同手機螢幕上都能執行良好–不同手機螢幕的解析度、尺寸並不完全相同,如果讓程式手動控制每個元件的大小,位置,則將給程式設計帶來巨大的困難。為了解決這個問題,Android提供了佈局管理器,佈局管理器可以根據執行平臺來調整元件的大小,我們要做的,是為容器選擇合適的佈局管理器。
與Swing介面程式設計不同的是,Android的佈局管理器本身就是一個UI元件,所以的佈局管理器都是ViewGroup的子類。下面顯示了Android佈局管理器的類圖。
這裡寫圖片描述


從圖片可以看出,所有的佈局都可以作為容器類使用,因此可以呼叫多個過載的addView()向佈局管理器中新增組。實際上,我們完全可以用一個佈局管理器巢狀到其他佈局管理器中–因為佈局管理器也繼承了View,也可以作為普通的UI元件使用。

線性佈局

線性佈局由LinearLayout類來代表,它們會將容器裡的元件一個一個的排列起來。 LinearLayout可以控制各元件橫向排列,也可以控制組件縱向排列。
Android 的線性佈局不會換行,當元件一個一個排列到頭時,剩下的元件不會被顯示出來

垂直線性佈局



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:layout_width="100dp" android:layout_height="wrap_content"
android:text="按鈕1"/>
/> <Button android:layout_width="100dp" android:layout_height="wrap_content" android:text="按鈕2"/> <Button android:layout_width="100dp" android:layout_height="wrap_content" android:text="按鈕3"/> </LinearLayout>

這裡寫圖片描述

水平線性佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="按鈕1"
    />

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="按鈕2"
    />
<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="按鈕3"
    />
<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="按鈕4"
    />
</LinearLayout>

這裡寫圖片描述

表格佈局

表格佈局由TableLayout代表,TableLayout繼承了LinearLayout,因此他的本質跟線性佈局相同
在表格佈局管理器中,可以為單元格設定3種行為方式

Shrinkable:如果某個列被設為 Shrinkable,那麼該列的所有單元格的寬度可以被收縮
Stretchable:如果某個列被設為Stretchable,那麼該列的所有單元格可以被拉伸
Collapsed:如果某個列被設為Collapsed,那麼該列的所有單元格會被隱藏

表格佈局

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     >
    <TableRow >
        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button1" />
    </TableRow>
    <TableRow >
        <Button
            android:id="@+id/button2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button2" />
    </TableRow>
    <TableRow >
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button3" />
    </TableRow>
    <TableRow >
        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button4" />
    </TableRow>
    <View
        android:layout_height="2dip"
        android:background="#FF909090" />
    <TableRow >
        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button5" />
        <Button
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button6" />
        <Button
            android:id="@+id/button7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button7" />
        <Button
            android:id="@+id/button8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button8" />
    </TableRow>
</TableLayout>

這裡寫圖片描述