windowmanager addview显示view

WindowManager的方法很简单,有三个方法,AddView(),removeView(),updateViewLayout();
AddView();    //添加View
removeView();            //移除VIew
updateViewLayout()    //更新View

View view = LayoutInflater.from(this).inflate(R.layout.dialog,null);    //加载View视图,这个就是我们要显示的内容
        WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);    //获取WindowManage
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        //设置LayoutParams的属性
        layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;       //该Type描述的是形成的窗口的层级关系,下面会详细列出它的属性
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |       //该flags描述的是窗口的模式,是否可以触摸,可以聚焦等
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        layoutParams.gravity = Gravity.CENTER;                                       //设置窗口的位置
        layoutParams.format = PixelFormat.TRANSLUCENT;                               //不设置这个弹出框的透明遮罩显示为黑色
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;                //窗口的宽             
        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;               //窗口的高  
        layoutParams.token = ((View)findViewById(R.id.linearlayout)).getWindowToken();           //获取当前Activity中的View中的TOken,来依附Activity,因为设置了该值,纳闷写的这些代码不能出现在onCreate();否则会报错。
        windowManager.addView(view,layoutParams);                                   //显示窗口

别忘了在AndroidManifest.xml中添加权限:

你可能感兴趣的:(调试示例源码)