如何创建Android中始终顶全屏覆盖活动 [英] How to create always-top fullscreen overlay activity in Android

查看:281
本文介绍了如何创建Android中始终顶全屏覆盖活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够创建一个活动,总是在Android的显示的前面。 它不应当得到的输入,只是把它传递到任何应用程序旁边下方。像一个平视显示器。

我可以研究,我需要设置基础窗口类型TYPE_SYSTEM_ALERT但它看起来像Android这样无视我的code - 也不例外,如果我删除清单android.permission.SYSTEM_ALERT_WINDOW许可,甚至抛出。 (它是需要使用此窗口类型)。当我试图用警报类型的对话框,它的工作好了,但我不能让对话为全屏幕透明的实体。 这是我的code,也许有一些简单的缺失。

 公共无效的onCreate(包savedInstanceState){
     super.onCreate(savedInstanceState);

     requestWindowFeature(Window.FEATURE_NO_TITLE);
     getWindow()setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN)。
     。getWindow()的setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
     。getWindow()setBackgroundDrawableResource(android.R.color.transparent);
     。getWindow()和setFormat(PixelFormat.TRANSLUCENT);
     的setContentView(R.layout.main);
}
 

半透明的设置必须在XML清单对外启用,否则它也没有工作。

 <项目名称=机器人:windowIsTranslucent>真< /项目>
 

解决方案

 最后WindowManager.LayoutParams PARAMS =新WindowManager.LayoutParams(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);

窗口管理WM =(窗口管理器)getApplicationContext()
    .getSystemService(Context.WINDOW_SERVICE);

ViewGroup中mTopV​​iew =(ViewGroup中)App.inflater.inflate(R.layout.main,NULL);
。getWindow()setAttributes(PARAMS);
wm.addView(mTopV​​iew,则params);
 

I'd like to be able to create an Activity that is always at the front of display of Android. It should receive no input, just pass it down to whatever application is next below it. Something like a HUD.

I was able to research that I need to set underlying window type to TYPE_SYSTEM_ALERT but it looks like Android is ignoring my code - no exception thrown even if I delete android.permission.SYSTEM_ALERT_WINDOW permission from manifest. (it is required to use this window type). When I tried to use ALERT type on dialog, it worked OK, but I cannot make dialog into full screen transparent entity. Here is my code, maybe there is something simple missing.

public void onCreate(Bundle savedInstanceState) {       
     super.onCreate(savedInstanceState);        

     requestWindowFeature(Window.FEATURE_NO_TITLE);
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
     getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);             
     getWindow().setBackgroundDrawableResource(android.R.color.transparent);
     getWindow().setFormat(PixelFormat.TRANSLUCENT);
     setContentView(R.layout.main);
}

Translucent setting has to be enabled externally in xml manifest, otherwise it also didn't work.

 <item name="android:windowIsTranslucent">true</item>

解决方案

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);

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

ViewGroup mTopView = (ViewGroup) App.inflater.inflate(R.layout.main, null);
getWindow().setAttributes(params);
wm.addView(mTopView, params);

这篇关于如何创建Android中始终顶全屏覆盖活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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