已经问过类似的问题,但运气不佳。
假设我有一个服务,我需要在它上面弹出一个 View 。同时,它们都应该是难以处理的,即用户应该能够在 View 中单击按钮,以及在后台服务中单击按钮。
这在理论上可能吗?如果是,我应该如何初始化该 View ?
谢谢!
最佳答案
是的,这是可能的,您需要做的是调用 WindowManager
服务并通过该服务添加您的 View 。
WindowManager windowManager=(WindowManager)getSystemService(WINDOW_SERVICE);
LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
RelativeLayout layout=(RelativeLayout) inflater.inflate(R.layout.box,null);
您需要一个 WindowManager.LayoutParams
对象,它应该包含布局参数
windowManager.addView(layout,params);
好了,添加 View
关于android - 从服务启动 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7521307/