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

Android 入門教程(七):五大版面容器一次搞懂

編輯:Android 開發實例

這一節把 Android 的五種基本版面容器一次講清楚:FrameLayout(框架版面)、LinearLayout(線性版面)、AbsoluteLayout(絕對版面)、RelativeLayout(相對版面)、TableLayout(表格版面)。

一、FrameLayout:最簡單的堆疊容器

FrameLayout 相當於螢幕上一塊空白備用區,你可以在裡面填東西。所有子元素都固定在左上角,無法 individually 指定位置;後加入的子元素會直接疊在前一個上面,把它部分或全部遮住(除非後者是透明的)。適合做浮層、遮罩這類需求。

[效果圖]

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="button" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="textview"
        android:textColor="#0000ff" />
</FrameLayout>

二、LinearLayout:單軸排列,靠 weight 分配空間

LinearLayout 依你設定的 orientation 把所有子元素排成一條直線。垂直排列時每一列只有一個元素,水平排列時只有一列。

它最實用的是 weight:指定權重後,子元素可以填滿剩下的空間,避免在大螢幕上一堆小物件擠成一團。預設權重為 0,也就是不放大。舉例來說,三個文字框中有兩個設 weight="1",這兩個就會等比例放大填滿剩餘空間,第三個維持原樣。

[效果圖]

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:orientation="vertical">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Welcome to Mr Wei's blog"
            android:textSize="15pt" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#aa0000"
            android:gravity="center_horizontal"
            android:text="red" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#00aa00"
            android:gravity="center_horizontal"
            android:text="green" />
    </LinearLayout>
</LinearLayout>

三、AbsoluteLayout:精準座標,但強烈不建議

AbsoluteLayout 讓子元素指定精確的 x/y 座標,左上角為 (0, 0),往右下移動數值變大。它沒有內距,也允許元素重疊。

實務上不推薦使用:寫死座標的版面太過剛性,換到不同尺寸或密度的裝置上幾乎一定走位。除非有非常明確的理由,否則請改用 RelativeLayout 或 ConstraintLayout。

[效果圖]

<AbsoluteLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Welcome to Mr Wei's blog" />

    <Button
        android:layout_width="70px"
        android:layout_height="wrap_content"
        android:layout_x="250px"
        android:layout_y="40px"
        android:text="Button" />
</AbsoluteLayout>

四、RelativeLayout:以相對關係定位

RelativeLayout 讓子元素指定「相對於其他元素或父容器」的位置(透過 id 關聯),可以輕鬆做到靠右對齊、上下排列或置中。元素是按順序解析的,所以用 XML 撰寫時,被引用的元素必須先定義。

[效果圖]

<?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="fill_parent">

    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Welcome to Mr Wei's blog:" />

    <EditText
        android:id="@+id/entry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/label" />

    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/entry"
        android:layout_marginLeft="10dip"
        android:text="OK" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/ok"
        android:layout_toLeftOf="@id/ok"
        android:text="Cancel" />
</RelativeLayout>

五、TableLayout:以列與欄排布

TableLayout 把子元素分配到列與欄。它由多個 TableRow 組成,每個 TableRow 定義一列,每列可擁有零個或多個 cell,每個 cell 放一個 View。容器不會顯示表格線;cell 可以是空的,但不能跨欄——這點與 HTML 的 table 不同。另外可用 android:stretchColumns 指定哪幾欄要自動拉伸填滿寬度。

[效果圖]

<?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="1">

    <TableRow>
        <TextView android:layout_column="1" android:text="Open..." />
        <TextView android:gravity="right" android:text="Ctrl-O" />
    </TableRow>

    <TableRow>
        <TextView android:layout_column="1" android:text="Save..." />
        <TextView android:gravity="right" android:text="Ctrl-S" />
    </TableRow>

    <!-- 分隔線 -->
    <View
        android:layout_height="2dip"
        android:background="#FF909090" />

    <TableRow>
        <TextView android:text="X" />
        <TextView android:text="Export..." />
        <TextView android:gravity="right" android:text="Ctrl-E" />
    </TableRow>

    <View
        android:layout_height="2dip"
        android:background="#FF909090" />

    <TableRow>
        <TextView
            android:layout_column="1"
            android:padding="3dip"
            android:text="Quit" />
    </TableRow>
</TableLayout>

選用建議

實務上的優先順序是:ConstraintLayout(能處理絕大多數版面,層級最淺)→ LinearLayout(單軸排列、權重分配)→ RelativeLayout(需要相對定位時)→ FrameLayout(單一子元素或堆疊場景)→ TableLayout(確實是表格資料時)。AbsoluteLayout 已標記為 deprecated,除非維護舊專案,否則不必再學。

版面層級越深,measure 與 layout 的開銷越大,這也是「能用 ConstraintLayout 平鋪就不用多層嵌套」的原因。

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