使用WindowManager添加您自己的自定义视图
在写手机卫士的时候,用户拨打|接听电话须要显示号码归属地,然后出现了一些异常,在此留下记号。希望对麻友们有帮助: BUG教程 在使用 view = View.inflate(this, R.layout.ui_toast, null);获得View对象后wm.addView出现错误: 10-1214:29:06.166:E/AndroidRuntime(1268):Causedby:android.view.WindowManager$BadTokenException:Unabletoaddwindowandroid.view.ViewRootImpl$W@41087ed8--permissiondeniedforthiswindowtype 解决的方法: 使用另外的方法获取: LayoutInflaterinflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); mDesktopLayout=inflater.inflate(R.layout.volume_panel,null); 完整代码: LayoutInflaterinflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); mDesktopLayout=inflater.inflate(R.layout.volume_panel,null); //取得系统窗口 mWindowManager=(WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE); //窗口的布局样式 mLayoutParams=newWindowManager.LayoutParams(); //设置窗口显示类型――TYPE_SYSTEM_ALERT(系统提示) mLayoutParams.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; //设置窗口焦点及触摸: //FLAG_NOT_FOCUSABLE(不能获得按键输入焦点) mLayoutParams.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; //设置显示的模式 mLayoutParams.format=PixelFormat.RGBA_8888; //设置对齐的方法 mLayoutParams.gravity=Gravity.TOP|Gravity.LEFT; //设置窗口宽度和高度 mLayoutParams.width=WindowManager.LayoutParams.WRAP_CONTENT; mLayoutParams.height=WindowManager.LayoutParams.WRAP_CONTENT; //设置窗口显示的位置。否则在屏幕中心显示 mLayoutParams.x=50; mLayoutParams.y=50; mWindowManager.addView(mDesktopLayout,mLayoutParams); 问题总结 这两种方式显示出来的窗体都不会激发当前Activity的onPause()事件。据说还能够先一个窗体风格的Activity。只是显示时会激发当前窗体的onPause()事件。详细咋样没试过。 推荐风格 <?xmlversion="1.0"encoding="utf-8"? ><resources> <style name="selectorDialog" parent="@Android:style/Theme.Dialog"> <itemname="android:windowFrame">@null</item><!--边框--> <itemname="android:windowIsFloating">true</item><!--是否浮如今activity之上--> <itemname="android:windowIsTranslucent">false</item><!--半透明--> <itemname="android:windowNoTitle">true</item><!--无标题--> <!--<itemname="android:windowBackground">@drawable/selector_dialog_bg</item>背景透明--> <itemname="android:backgroundDimEnabled">false</item><!--模糊--> <itemname="android:backgroundDimAmount">0.6</item> </style></resources> OK,谢谢关注破晓博客 版权声明:本文博客原创文章。博客,未经同意,不得转载。 本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4734812.html,如需转载请自行联系原作者