我想在每个窗口的顶部构建一个视图。
我找到了这段代码:
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"/>
视图已构建,但是当我按下主页按钮时,除状态栏外无法看到任何内容。
怎么了?请帮帮我。
答案 0 :(得分:0)
更改
wmgr = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
到
wmgr = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);