編輯:關於 Android 程式設計
幀佈局會為其中的每個元件開闢一塊空白區域(稱為「幀」),這些幀一層層疊加在一起,有點像一張張覆蓋貼上的海報,後加入的元件會把前面的元件遮住。
FrameLayout 有兩個較為特殊且常用的屬性值得注意:
對應方法:setForeground(Drawable)
說明:用來設定幀佈局的前景圖像。一般為佈局加入 pressed 狀態時,會用這個屬性來指定一個 Drawable 物件。
舉個例子:
<FrameLayout android:foreground="@drawable/muogu" android:layout_height="fill_parent" android:layout_width="fill_parent">
<TextView android:background="#3399AA" android:height="320dp" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:width="320dp">
<TextView android:background="#AA77AA" android:height="280dp" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:width="280dp">
<TextView android:background="#662288" android:height="240dp" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:width="240dp">
<TextView android:background="#895511" android:height="200dp" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:width="200dp">
<ImageView android:layout_gravity="center" android:layout_height="160dp" android:layout_width="160dp" android:scaleType="fitXY" android:src="@drawable/nailiu16">
</ImageView>
</TextView>
</TextView>
</TextView>
</TextView>
</FrameLayout>

對應方法:setForegroundGravity(int)
說明:顧名思義,它與前景圖像的擺放有關。沒錯,這個屬性的功能就是定義前景圖像的 gravity,因此必須搭配 foreground 一起使用。其可選值與 gravity 相同,可在先前的《Android UI 之 LinearLayout(線性佈局)》中找到對應的屬性表。
舉個例子:
<FrameLayout android:foreground="@drawable/muogu" android:foregroundGravity="bottom|right" android:layout_height="fill_parent" android:layout_width="fill_parent">
<TextView android:background="#3399AA" android:height="320dp" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:width="320dp">
<TextView android:background="#AA77AA" android:height="280dp" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:width="280dp">
<TextView android:background="#662288" android:height="240dp" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:width="240dp">
<TextView android:background="#895511" android:height="200dp" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:width="200dp">
<ImageView android:layout_gravity="center" android:layout_height="160dp" android:layout_width="160dp" android:scaleType="fitXY" android:src="@drawable/nailiu16">
</ImageView>
</TextView>
</TextView>
</TextView>
</TextView>
</FrameLayout>

這裡的程式碼與上一段唯一的差別,就是在 FrameLayout 中多加了下面這行:
android:foregroundGravity="bottom|right"
可以看到前景圖像恢復了原有大小,並依設定被放到了右下角,這樣 FrameLayout 中的子元件就正常顯示出來了。
Android 知識梳理:屏幕適配完整攻略
Android 的碎片化問題困擾著幾乎所有開發者,其中屏幕適配是最頭疼的一塊。想讓 App 在成千上萬種設備上都有體面的顯示效果,適配必須做到位。本文結合官方文檔與個人實
Android 開發中的記憶體洩漏檢測
一、記憶體洩漏 記憶體洩漏會因可用記憶體的不斷減少而拖垮系統效能,最糟的情況下,過量記憶體被佔用會導致部分或全部裝置停止運作,甚至讓 App 直接崩潰。 並非所有洩漏都很
EditText 輸入監聽與表單驗證:可複用的 TextWatcher 設計
Android design support 包提供了一種在輸入不合法時持續顯示提示的方式,如今已被越來越多應用採用。不過各家 App 呈現錯誤提示的手法五花八門,放在
深入解析 Android 綁定式 Service(Bound Service)
綁定式 Service(Bound Service)在客戶端/伺服端(CS)架構中扮演的是 Server 的角色。它允許其他元件(例如 Activity)前來綁定、送出請