編輯:關於 Android 程式設計
一般來說,Android 顯示一個 View 都是透過 Activity 的 setContentView 來設定。但其實還有一條路:直接用 WindowManager 把 View 畫在整個應用的最上層,整體效果近似 AlertDialog 的彈出體驗。
做法並不難,核心就是 windowmanager.addView() 這個方法。
package com.gearmotion.app.windowmanagermotion;
import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
public class MainActivity extends AppCompatActivity
implements View.OnClickListener, View.OnTouchListener {
Button mShowBtn;
Button mHideBtn;
WindowManager mWm;
LayoutInflater mLayoutInflater;
View mWindowView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mShowBtn = (Button) this.findViewById(R.id.showbtn);
mHideBtn = (Button) this.findViewById(R.id.hidebtn);
mShowBtn.setOnClickListener(this);
mHideBtn.setOnClickListener(this);
init();
}
private void init() {
mWm = (WindowManager) this.getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
mLayoutInflater = LayoutInflater.from(this);
}
@Override
public void onClick(View v) {
if (mShowBtn.hashCode() == v.hashCode()) {
show();
}
if (mHideBtn.hashCode() == v.hashCode()) {
hide();
}
}
private void show() {
mWindowView = mLayoutInflater.inflate(R.layout.item_layout, null);
View popView = mWindowView.findViewById(R.id.root);
// 設置 popView 的觸控事件,以便點擊空白區域時讓懸浮 view 消失
popView.setOnTouchListener(this);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
// 窗口類型同系統彈出框
lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
// 透明層
lp.format = PixelFormat.TRANSPARENT;
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
lp.gravity = Gravity.CENTER_VERTICAL;
mWm.addView(mWindowView, lp);
}
private void hide() {
if (mWindowView != null && mWindowView.getParent() != null) {
mWm.removeView(mWindowView);
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
// 獲取主 view 的可視區域
Rect globalRect = new Rect();
// 獲取懸浮 view 的可視區域
Rect tmpRect = new Rect();
v.getGlobalVisibleRect(globalRect);
View child = ((ViewGroup) v).getChildAt(0);
child.getHitRect(tmpRect);
// 點在懸浮 view 之外、但在窗口範圍之內,就關閉它
if (!tmpRect.contains(x, y) && globalRect.contains(x, y)) {
hide();
}
return true;
}
}
這裡有幾個配置值得留意:lp.type 設為 TYPE_SYSTEM_ALERT 讓窗口行為接近系統彈窗;lp.format 用 PixelFormat.TRANSPARENT 保持透明底;而 onTouch 中透過 getHitRect 判斷觸點是否落在內容區之外,就能實現「點空白處關閉」的常見互動。
<?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"
tools:context=".MainActivity">
<Button
android:id="@+id/showbtn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="show" />
<Button
android:id="@+id/hidebtn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="hide" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/root"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="I am WindowManager layout view"
android:textSize="20sp"
android:gravity="center"
android:layout_gravity="center"
android:background="#FFF8DC"
android:textColor="#7AC5CD" />
</LinearLayout>
成果如下:

以上就是本文的全部內容,希望對大家學習 Android 有所幫助,也歡迎多多支持本站。
Android 計算器介面布局:以 LinearLayout 打造按鍵與顯示區
計算器是 Android 入門練習中很常見的介面題型。它通常包含一個顯示結果的輸入區,以及一組數字與運算按鈕。本文以 LinearLayout 為例,說明如何安排顯示區與
Android Dialog 實戰大全:從系統彈窗到自定義底部菜單
做 Android 開發,Dialog 幾乎是繞不開的一塊。提示確認要用它,底部彈出的操作面板要用它,仿 Toolbar 菜單也要用它。但 Dialog 的玩法實在太多,
開源 Android 文件管理器:功能與專案結構
這是一個開源的 Android 文件管理器範例,重點在於展示如何組織檔案瀏覽、操作與 UI 元件。專案採用模組化方式,將介面、資料操作與工具類分開管理。 主要功能 新建
Android 四大組件入門:Activity 全解析
Activity 是 Android 應用中最常見的四大組件之一,負責承接使用者可見的介面,並管理頁面的建立、顯示、暫停與銷毀。它也是許多人接觸 Android 開發時,