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

Android UI 之 FrameLayout(幀佈局)详解

編輯:關於 Android 程式設計

FrameLayout 幀佈局介紹

幀佈局會為其中的每個元件開闢一塊空白區域(稱為「幀」),這些幀一層層疊加在一起,有點像一張張覆蓋貼上的海報,後加入的元件會把前面的元件遮住。

FrameLayout 有兩個較為特殊且常用的屬性值得注意:

1. android:foreground

對應方法: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>

2. foregroundGravity

對應方法: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 中的子元件就正常顯示出來了。

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