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

Android 布局详解:FrameLayout 完全指南

編輯:關於 Android 程式設計

修正說明:

這是我寫的第一篇文章,當時確實考慮得不够周全。後來一直沒有回頭檢視,看到評論多為負面,也就擱置了這個系列。今天重新把文章修訂一遍,不敢說完全沒有錯,只是把當年漏寫的內容補上。評論不刪不改,供大家參考。

我的文章基本面向新手,因此沒有很多高深的玩法(我自己也不擅長)。新手看我的文章入門即可,更深奧的內容不在這裡。

再版修正說明:

首先要感謝指出錯誤的朋友。上一篇修正說明寫得借口較多、忘了道歉,態度不好,請多包涵。特別感謝指出問題的朋友,這篇文章確實寫得不夠嚴謹,遇到問題一筆帶過,造成了誤解,非常抱歉。

這次重新修正了 android:layout_width="fill_parent" 屬性導致 android:layout_gravity 失效的問題。

FrameLayout 是最簡單的布局之一。所有放在其中的控制項,都會按層次堆疊在螢幕的左上角,後加入的控制項會覆蓋前面的控制項。在 FrameLayout 中,定義控制項位置相關的屬性原則上意義不大,控制項會自動堆放在左上角。看以下範例:

 <?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="50sp" android:textColor="#000000" android:text="第一層"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="40sp" android:textColor="#ffff00" android:text="第二層"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="30sp" android:textColor="#ff00ff" android:text="第三層"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#00ffff" android:text="第四層"/> </FrameLayout>

效果如下圖:

變化 1

我們嘗試改變它們的位置,把第一、二個文字方塊改成讓第二個放在第一個右邊:

 <TextView android:id="@+id/tv1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="50sp" android:textColor="#000000" android:text="第一層"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="40sp" android:textColor="#ffff00" android:layout_toRightOf="@id/tv1" android:text="第二層"/>

看到了沒?畫面還是一樣不變,layout_toRightOf 並沒有生效。

變化 2

我們嘗試 android:gravity 屬性,把第三個文字方塊改成:

 <TextView android:id="@+id/tv3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="30dip" android:textColor="#ff00ff" android:gravity="right" android:text="第三層"/>

看效果:竟然沒有覆蓋,而是錯開了!

先別大驚小怪,這並不代表 FrameLayout 失效了。gravity 屬性是控制控制項內部文字格式的。而我們控制項的寬度屬性是 fill_parent,也就是文字方塊寬度等於螢幕寬度,因此 android:gravity="right" 只是讓文字靠右,文字方塊本身仍舊從左上角堆疊。不信,我們把寬度改為自適應:

 <TextView android:id="@+id/tv3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30dip" android:textColor="#ff00ff" android:gravity="right" android:text="第三層"/>

這下是不是打回原形了?

變化 2 +(layout_gravity 的作用)

這是原文被指正最多的地方。原文曾寫「FrameLayout 根本無法控制其子控制項的位置」,這句話其實有誤——子控制項可以透過 android:layout_gravity 屬性來控制自己在父控制項中的位置。

打上程式碼:

 <TextView android:id="@+id/tv3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="30dip" android:textColor="#ff00ff" android:layout_gravity="right" android:text="第三層"/>

效果與第一張圖相同,看似 android:layout_gravity="right" 沒起作用。其實是因為 android:layout_width="fill_parent" 造成的——文字方塊寬度充滿父控制項,所以文字不會靠右。改成:

 <TextView android:id="@+id/tv3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30dip" android:textColor="#ff00ff" android:layout_gravity="right" android:text="第三層"/>

這下 android:layout_gravity="right" 就生效了。

變化 3

有回帖提到用 android:layout_gravity="center_horizontal|bottom",我們來試試:

 <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#00ffff" android:layout_gravity="center_horizontal|bottom" android:text="第四層"/>

效果如下圖:

可以看到第四層跑到底部來了,但 center_horizontal 沒起作用。這個問題同樣是因為 android:layout_width="fill_parent" 造成的。改成:

 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#00ffff" android:layout_gravity="center_horizontal|bottom" android:text="第四層"/>

第四層就居中了。

總結一下,經過以上實驗可知:在 FrameLayout 中,預設所有控制項都是左上對齊;控制項可透過 android:layout_gravity 屬性控制自己在父控制項中的位置,但前提是寬高設為 wrap_content,否則 layout_gravity 會因 fill_parent 而失效。

那麼這麼簡單的 Layout 有什麼用?我認為它還是有存在價值的:當你需要自己寫一個 View,並在內部已完成所有邏輯(例如遊戲)時,這個 View 只需要一個容器來放置,就可以使用 FrameLayout。雖然其他布局也能勝任,但用最簡單的,不是更省系統資源嗎?

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