在所有活动之上添加视图 [英] Add a view on top of all the Activities

查看:94
本文介绍了在所有活动之上添加视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用程序.我希望能够通过代码添加一个视图,该视图绘制在应用程序的所有活动的顶部.

I'm developing an Android Application. I want to be able to add one view by code, that is drawn in top of all the activities on the application.

我试图将其添加到窗口管理器中:

I've tried to add it to the window manager:

LayoutInflater inflater = activity.getLayoutInflater();
layout = inflater.inflate(R.layout.toast_layout, null);

WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.gravity = Gravity.BOTTOM;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.type = WindowManager.LayoutParams.TYPE_TOAST;
final WindowManager mWindowManager = (WindowManager);
activity.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
                mWindowManager.addView(layout, params);

但是,像这样添加它,我会遇到两个问题:

However, adding it like this I face two problems:

1.退出应用程序时,布局仍然显示.

1.The layout is still displayed when I exit my app.

2.布局不响应Click事件.

2.The layout does not respont to Click events.

是否有另一种解决方案来实现这一目标?

Is there another solution to achieve this?

谢谢.

推荐答案

为了管理视图的显示/视图,我使用了Gopal提出的解决方案. 我附加了onStop和onResume事件,以便在退出应用程序时隐藏视图.

In order to manage the show/view of the view I've used the solution proposed by Gopal. I've attached onStop and onResume events so that the view is hidden when exiting application.

对于单击事件,我已经意识到类型吐司不响应单击事件.因此,我更改了类型

For the click events, I've realized that the type toast does not respond to click events. Therefore I've changed the type

params.type = WindowManager.LayoutParams.TYPE_TOAST;

params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

我也必须添加以下权限:

I've to add the following permision too:

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

这篇关于在所有活动之上添加视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆