WindowManager :悬浮层 它的View显示在整个系统的最顶层,在设置属性后,此View则可以存在手机主窗口上
给windowManager添加overlay这个视图
LayoutInflater inflater = LayoutInflater.from(this); overlay = (TextView) inflater.inflate(R.layout.overlay, null); overlay.setVisibility(View.INVISIBLE); WindowManager.LayoutParams lp = new WindowManager.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT); windowManager = (WindowManager)this.getSystemService(Context.WINDOW_SERVICE); windowManager.addView(overlay, lp);
但是当我关闭此activity时会报如下错误:
12801-12801/com.wjf.fw.mybus E/WindowManager﹕ Activity com.wjf.fw.mybus.ChangeCityActivity has leaked window android.support.v7.widget.AppCompatTextView{4279d760 I.ED.... ......I. 0,0-130,186} that was originally added here
android.view.WindowLeaked: Activity com.wjf.fw.mybus.ChangeCityActivity has leaked window android.support.v7.widget.AppCompatTextView{4279d760 I.ED....
虽然此错误不影响应用使用,但毕竟是错误,应该去解决:当销毁activity时应去掉WindowManager的视图
@Override protected void onDestroy() { super.onDestroy(); windowManager.removeView(overlay); }