Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 從無知到有知(NO.3):五種常見佈局速覽

編輯:關於 Android 程式設計

昨天把幾種常見佈局看了一遍,感覺跟以前學的 HTML + CSS 有點像,只是用 XML 描述。這幾種都不算難,邏輯想清楚就能排出來。

相對佈局 RelativeLayout

我們平常最常用的一種,先前做的「電話撥號器」和「短信發送器」都用它。核心概念是描述控制項之間的相對位置:誰在誰的下方、誰相對於父容器靠右。只要有兩個控制項,一個 id 為 tv、一個 id 為 xc,要讓 xc 排在 tv 下方,只需在 xc 上寫:

android:layout_below="@id/tv"

一眼就懂。當然也可以用 layout_marginLeft 這類具體數值微調,或搭配 layout_alignParentRight、layout_centerVertical 等對齊父容器的屬性。

線性佈局 LinearLayout

比相對佈局更簡單,就是橫平豎直:所有控制項排在一條線上,靠 orientation 決定是橫排還是直排。實務上常與相對佈局混用——例如系統設定頁那種「一行文字 + 右側單選框」的樣式,外層 LinearLayout 排一列列項目,每列內部再用相對佈局對齊。

表格佈局 TableLayout

顧名思義,用列(row)與行(column)來排版,很適合資訊填寫類的表單:姓名、年齡、性別、身分證號等等。效果與多層 LinearLayout 相近,但語意更清楚,配合 android:stretchColumns 還能自動分配欄寬。

絕對佈局 AbsoluteLayout

用 layout_x 與 layout_y 直接指定控制項相對於父容器的座標。像早期的 QQ 遊戲「歡樂鬥地主」,撲克牌的位置就是用絕對座標定位的。但這種佈局已經被棄用,原因很現實:Android 裝置螢幕尺寸百百種,寫死座標在不同解析度上必然錯位。現在的正確做法是動態取得螢幕寬高,再用百分比換算位置。

幀佈局 FrameLayout

這是我認為最有趣的一種:它像洋蔥一樣,由一層一層堆疊而成,後加入的子 View 疊在上層。最經典的應用就是影片播放器的暫停圖示——影片畫面在上,中央疊一個半透明的播放按鈕。

先看版面:

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="播放視頻中"
        android:gravity="center"
        android:visibility="visible" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
        <Button
            android:onClick="play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="播放" />
        <Button
            android:onClick="pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="暫停" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >
        <ImageView
            android:id="@+id/iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:visibility="invisible" />
    </LinearLayout>
</FrameLayout>

三個區塊由下而上堆疊:播放中的提示文字、兩個按鈕、以及暫停時才出現的中央圖示。幾個關鍵屬性:

visibility:控制是否顯示,後面用它切換圖示狀態。visible 顯示、invisible 佔位但隱形、gone 連版面空間都不佔。

src="@drawable/ic_launcher":圖片來源,可自訂。

onClick="pause":直接指派點擊事件方法,省去 findViewById 與 setOnClickListener。

接著是 Activity:

public class MainActivity extends Activity {

    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_frame);
        iv = (ImageView) findViewById(R.id.iv);
    }

    public void play(View view) {
        iv.setVisibility(View.INVISIBLE);
    }

    public void pause(View view) {
        iv.setVisibility(View.VISIBLE);
    }
}

先取得圖示物件,然後:收到「播放」就隱藏圖示,收到「暫停」就顯示。

前者是播放中的狀態,後者是暫停時的狀態。

選用的邏輯

實務上的判斷很簡單:表單類用 TableLayout、清單列用 LinearLayout、跨維度對齊用 RelativeLayout(新版可用 ConstraintLayout 取代)、需要層疊遮罩效果才用 FrameLayout。層數越深效能開銷越大,能壓在兩層以內就不要放到第三層。今天先這樣。

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