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

從零開始學 Android(十四):FrameLayout 幀布局詳解

編輯:關於 Android 程式設計

FrameLayout(幀布局)的工作方式很單純:它在屏幕上開闢一個矩形區域,所有放入的子組件預設都會被擺到左上角,並且彼此可以重疊層疊。這個「疊加」特性正是它名字裡「幀」的由來——就像動畫的一格一格畫面疊在一起。

一、XML 配置

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="322dp"
        android:src="@drawable/KILL" />

    <Button
        android:id="@+id/button1"
        android:layout_width="240dp"
        android:layout_height="107dp"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="154dp"
        android:layout_height="62dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</FrameLayout>

效果圖中可以看到:三個子元素全部從左上角 (0, 0) 開始排布,後加入的會蓋在先加入的上面。FrameLayout 中子元素的先後順序決定層疊次序,這一點調試時經常被忽略。

二、純 Java 動態配置

package com.example.framelayout;

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FrameLayout layout = new FrameLayout(this);

        // 布局本身佔滿全屏
        FrameLayout.LayoutParams rootParams = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);

        // 各子元素的尺寸
        FrameLayout.LayoutParams imageParams =
                new FrameLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, 311);
        FrameLayout.LayoutParams buttonParams =
                new FrameLayout.LayoutParams(281, 173);
        FrameLayout.LayoutParams textParams =
                new FrameLayout.LayoutParams(183, 85);

        ImageView imageView = new ImageView(this);
        imageView.setImageResource(R.drawable.kill);
        layout.addView(imageView, imageParams);

        Button button = new Button(this);
        button.setText("button");
        layout.addView(button, buttonParams);

        TextView textView = new TextView(this);
        textView.setText("TextView");
        layout.addView(textView, textParams);

        setContentView(layout, rootParams);
    }
}

三、兩種方式的效果一致,怎麼選?

對比兩張運行結果圖,動態代碼與 XML 配置實現的視覺效果完全相同。選擇依據在於場景:

  • 界面結構固定、需要預覽和快速微調 → 用 XML,Android Studio 的 Layout Editor 能即時渲染。
  • 子元素數量或內容依運行時數據決定 → 用代碼動態生成。

兩者並非互斥,實務上常見的混合做法是:XML 定骨架,代碼填內容。

四、實戰技巧:用 FrameLayout 做浮層

FrameLayout 真正的用武之地是「疊加」,典型場景有三類:

  1. 圖片 + 文字水印:底層 ImageView,上層 TextView 並設 layout_gravity="bottom|end"。
  2. 加載遮罩:底層內容,上層半透明背景 + ProgressBar,通過控制上層可見性顯示/隱藏 loading。
  3. 自定義對話框式彈層:不需要真正彈出 Dialog,直接在頁面內疊一層。

要讓子元素靠對齊而非左上角,記得加 android:layout_gravity,例如 center、bottom|right。這是 FrameLayout 不同於其他容器的一大特性——它支援每個子元素獨立指定重力對齊。

另外,foreground 與 foregroundGravity 屬性可以在整個 FrameLayout 之上再疊一層前景 Drawable,做「列表頂部漸變陰影」這類效果時非常方便。

下節預告:表格布局 TableLayout。

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