如何在Android上的所有其他窗口的顶部构建一个窗口

时间:2011-12-12 15:00:22

标签: android window android-windowmanager

我想在每个窗口的顶部构建一个视图。

我找到了这段代码:

bt = new Button(getApplicationContext());
LayoutParams param = new LayoutParams();
param.flags = LayoutParams.FLAG_NOT_FOCUSABLE;
param.format = PixelFormat.RGBA_8888;
param.type = LayoutParams.TYPE_SYSTEM_ALERT;
param.gravity = Gravity.TOP | Gravity.LEFT;
param.width = 40;
param.height = 40;
wmgr = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
wmgr.addView(bt, param);

manifest.xml

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

视图已构建,但是当我按下主页按钮时,除状态栏外无法看到任何内容。

怎么了?请帮帮我。

1 个答案:

答案 0 :(得分:0)

更改

wmgr = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 

wmgr = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);