Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

安卓佈局入門:LinearLayout、TableLayout 與 RelativeLayout 實戰對照

編輯:Android 開發實例

寫 Android 介面,第一步永遠是選對佈局容器。Android 內建的常用佈局有三種思路:LinearLayout 沿著一條線排列、TableLayout 用表格對齊、RelativeLayout 靠相對位置定位。選錯容器,後面就得靠大量巢狀與魔法數字硬湊,維護起來非常痛苦。

LinearLayout 與常用屬性

LinearLayout 顧名思義是「一條線上的排列」,透過 android:orientation 決定水平(horizontal)或垂直(vertical)。搭配 layout_weight 可以按比例分配剩餘空間,這是它最實用的特性。

先認識幾個幾乎每個控制項都會用到的屬性:

android:id —— 為控制項指定 ID

android:text —— 控制項顯示的文字,盡量引用 strings.xml 而非寫死字串

android:gravity —— 控制項內部內容的基本位置,例如居中、靠右

android:textSize —— 字級大小

android:background —— 背景色,RGB 命名法

android:layout_width / android:layout_height —— 寬與高

android:padding* —— 內距,也就是內容與邊框之間的距離

android:singleLine —— 設為 true 時,內容強制顯示在同一行

<TextView

android:id="@+id/firstText"

android:text="第一行"

android:gravity="center_vertical"

android:textSize="35pt"

android:background="#aa0000"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:paddingLeft="10dip"

android:paddingTop="20dip"

android:paddingRight="30dip"

android:paddingBottom="40dip"

android:layout_weight="1"

android:singleLine="true"/>

這裡要特別區分 gravity 與 layout_gravity:前者控制「控制項內部內容」的對齊,後者控制「控制項自己在父容器內」的對齊,兩者常被混用而導致版面跑掉。

LinearLayout 巢狀 LinearLayout

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<LinearLayout

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1">

<TextView

android:text="red"

android:gravity="center_horizontal"

android:background="#aa0000"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1"/>

<TextView

android:text="green"

android:gravity="center_horizontal"

android:background="#00aa00"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1"/>

<TextView

android:text="blue"

android:gravity="center_horizontal"

android:background="#0000aa"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1"/>

<TextView

android:text="yellow"

android:gravity="center_horizontal"

android:background="#aaaa00"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1"/>

</LinearLayout>

<LinearLayout

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1">

<TextView

android:text="row one"

android:textSize="15pt"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"/>

<TextView

android:text="row two"

android:textSize="15pt"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"/>

<TextView

android:text="row three"

android:textSize="15pt"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"/>

<TextView

android:text="row four"

android:textSize="15pt"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"/>

</LinearLayout>

</LinearLayout>

這段結構很值得細看:外層是垂直、內層是水平,兩層各給 layout_weight="1",於是畫面上下各佔一半;上半部四個彩色方塊又各自 weight=1,因此等分寬度。巢狀 LinearLayout 就是靠這種「外層切一維、內層切另一維」的組合,拼出幾乎任何網格。

TableLayout 表格佈局

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:stretchColumns="0">

<TableRow>

<TextView

android:text="@string/row1_column1"

android:background="#aa0000"

android:padding="3dip" />

<TextView

android:text="@string/row1_column1"

android:padding="3dip"

android:gravity="center_horizontal"

android:background="#00aa00"

></TextView>

<TextView

android:text="@string/row1_column2"

android:gravity="right"

android:background="#0000aa"

android:padding="3dip" />

</TableRow>

<TableRow>

<TextView

android:text="@string/row2_column1"

android:padding="3dip" />

<TextView

android:text="@string/row2_column2"

android:gravity="right"

android:padding="3dip" />

</TableRow>

</TableLayout>

TableLayout 以 TableRow 定義每一列,android:stretchColumns="0" 表示第 0 欄可以拉伸填滿剩餘寬度——少了這個屬性,欄位寬度會依內容而定,看起來歪歪的。這是 TableLayout 最常被遺漏的一行。

LinearLayout 鑲 TableLayout

android:layout_weight="1" 的意義就是「把這個 layout 分幾份,數字代表佔幾份」。把兩種佈局混搭,就能做出左半是色塊、右半是表格的版面:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent">

<LinearLayout

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1">

<TextView

android:text="red"

android:gravity="center_horizontal"

android:background="#aa0000"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1" />

<TextView

android:text="green"

android:gravity="center_horizontal"

android:background="#00aa00"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1" />

<TextView

android:text="blue"

android:gravity="center_horizontal"

android:background="#0000aa"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1" />

<TextView

android:text="yellow"

android:gravity="center_horizontal"

android:background="#aaaa00"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1" />

</LinearLayout>

<LinearLayout

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1">

<TableLayout

xmlns:android="https://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:stretchColumns="0">

<TableRow>

<TextView

android:text="@string/row1_column1"

android:padding="3dip" />

<TextView

android:text="@string/row1_column1"

android:padding="3dip"

android:gravity="center_horizontal">

</TextView>

<TextView

android:text="@string/row1_column2"

android:gravity="right"

android:padding="3dip" />

</TableRow>

<TableRow>

<TextView

android:text="@string/row2_column1"

android:padding="3dip" />

<TextView

android:text="@string/row2_column2"

android:gravity="right"

android:padding="3dip" />

</TableRow>

</TableLayout>

</LinearLayout>

</LinearLayout>

RelativeLayout 相對佈局

相對佈局以「控制項的方位」為依據排列,適合用來做表單、懸浮按鈕這類元素關係明確的版面。屬性大致分兩組:一組是相對於其他控制項,一組是相對於父容器。

android:layout_above 將該控制項的底部置於給定 ID 控制項之上

android:layout_below 將該控制項的頂部置於給定 ID 控制項之下

android:layout_toLeftOf 將該控制項的右邊緣與給定 ID 控制項的左邊緣對齊

android:layout_toRightOf 將該控制項的左邊緣與給定 ID 控制項的右邊緣對齊

android:layout_alignBaseline 該控制項的 baseline 與給定 ID 控制項的 baseline 對齊

android:layout_alignBottom 將該控制項的底部邊緣與給定 ID 控制項的底部邊緣對齊

android:layout_alignLeft 將該控制項的左邊緣與給定 ID 控制項的左邊緣對齊

android:layout_alignRight 將該控制項的右邊緣與給定 ID 控制項的右邊緣對齊

android:layout_alignTop 將該控制項的頂部邊緣與給定 ID 控制項的頂部邊緣對齊

android:layout_alignParentBottom 為 true 時,該控制項底部與父容器底部對齊

android:layout_alignParentLeft 為 true 時,該控制項左邊與父容器左邊對齊

android:layout_alignParentRight 為 true 時,該控制項右邊與父容器右邊對齊

android:layout_alignParentTop 為 true 時,該控制項頂部與父容器頂部對齊

android:layout_centerHorizontal 為 true 時,置於水平中央

android:layout_centerInParent 為 true 時,置於父容器的水平與垂直中央

android:layout_centerVertical 為 true 時,置於垂直中央

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:padding="10px" >

<TextView android:id="@+id/label"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Type here:" />

<EditText android:id="@+id/entry"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="@android:drawable/editbox_background"

android:layout_below="@id/label" />

<Button android:id="@+id/ok"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/entry"

android:layout_alignParentRight="true"

android:layout_marginLeft="10px"

android:text="OK" />

<Button android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toLeftOf="@id/ok"

android:layout_alignTop="@id/ok"

android:text="Cancel" />

</RelativeLayout>

相對佈局有個硬性規則:被參照的控制項必須先宣告。以上例而言,Cancel 按鈕用 layout_toLeftOf="@id/ok" 定位,因此 OK 按鈕一定要寫在它前面,否則會出現「找不到 ID」的錯誤。另外 RelativeLayout 的效能通常優於多層巢狀的 LinearLayout,因為它不需要量測兩次,因此在版面較複雜時值得優先考慮。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved