關於WindowManager的一個小問題was originally added here
阿新 • • 發佈:2019-02-19
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); }