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

FrameLayout 詳解:層疊、layout_gravity 與前景圖像

編輯:關於 Android 開發

先看示範:

它是怎麼排列的

FrameLayout 是最簡單的布局形式。所有加入這個布局的視圖都以層疊方式顯示:第一個加入的放在最底層,最後一個加入的顯示在最頂層,上層會覆蓋下層,有點像堆疊。

預設情況下,所有子元件都會被放在左上角。幀布局的大小由子元件中最大的那一個決定;如果所有元件一樣大,同一時刻就只能看到最上面那個。

一、layout_gravity

FrameLayout 本身無法控制子元件的位置,但子元件可以用 android:layout_gravity 指定自己在父容器中的對齊方式。

二、layout_margin

在 FrameLayout 裡單獨設定 layout_margin 是不會生效的,必須搭配 layout_gravity 一起用。可以這樣理解:layout_gravity 決定參考點,最終位置由 layout_gravity 與 layout_margin 共同決定。

若希望元件正常顯示,建議把 layout_gravity 設為 top,以螢幕左上角為參考點。

三、前景圖像

前景圖像永遠處於幀布局最頂端、直接面對使用者,也就是不會被其他子元件覆蓋的那張圖。

常用屬性:

  • android:foreground:設定前景圖像
  • android:foregroundGravity:設定前景圖像顯示的位置

幀布局在遊戲開發中用得比較多。當你自寫一個 View、在裡面完成自己的邏輯(例如遊戲本體),這個 View 只需要一個容器來承載,用 FrameLayout 剛好——其他布局也行,但用最簡單的能省下系統資源。

四、應用實例

Activity 程式碼:監聽 FrameLayout 的觸碰事件,讓按鈕跟著手指跑。

package mm.shandong.com.testframelayout;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

public class TestFrameLayoutActivity extends AppCompatActivity {
    FrameLayout frameLayout;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_frame_layout);

        btn = (Button) findViewById(R.id.btn);
        frameLayout = (FrameLayout) findViewById(R.id.frameLayout);

        frameLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                int x = (int) motionEvent.getX();
                int y = (int) motionEvent.getY();
                int width = btn.getWidth();
                int height = btn.getHeight();
                btn.layout(x, y, x + width, y + height);
                return false;
            }
        });
    }
}

版面程式碼:

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下面是一個 FrameLayout,預設層疊" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="#ff0000">
        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="第一層" android:textColor="#00ff00" android:textSize="55sp" />
        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="第二層" android:textColor="#0000ff" android:textSize="45sp" />
        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="第三層" android:textColor="#00ffff" android:textSize="35sp" />
        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="第四層" android:textColor="#ffff00" android:textSize="25sp" />
    </FrameLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下面用 layout_gravity 與 margin 調位置,並設定前景圖" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:background="#ff0000"
        android:foreground="@drawable/red"
        android:foregroundGravity="right|bottom">
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_gravity="top|left"
            android:layout_marginLeft="20dp" android:layout_marginTop="0dp"
            android:text="左上 left:20 Top:0" android:textColor="#00ff00" android:textSize="15sp" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_gravity="top|left"
            android:layout_marginLeft="20dp" android:layout_marginTop="25dp"
            android:text="左上 left:20 Top:25" android:textColor="#00aa00" android:textSize="15sp" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_gravity="bottom|left"
            android:layout_marginBottom="20dp" android:layout_marginLeft="45dp"
            android:text="左下 left:45 Bottom:20" android:textColor="#0000ff" android:textSize="15sp" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="居中" android:textColor="#00ffff" android:textSize="15sp" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_gravity="right|top" android:layout_marginTop="30dp"
            android:text="右上 top 30" android:textColor="#ffff00" android:textSize="15sp" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_gravity="right|bottom" android:layout_marginBottom="80dp"
            android:text="最上層,仍被遮蓋" android:textSize="15sp" />
    </FrameLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下面用 layout 方法指定確切位置,請點擊此區域" />

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff0000">
        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="請點擊別處" />
    </FrameLayout>
</LinearLayout>

最後一個例子說明了另一件事:即使是層疊布局,也能透過 layout() 直接指定子元件的四邊座標,做出完全自訂的定位效果——這在開發拖曳類互動時很實用。

本文範例整理自安卓無憂,可至應用寶或豌豆莢下載「例子源碼」,源碼與文件一網打盡。

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