java代码:'
@Override public void setContentView(View view,ViewGroup.LayoutParams params) { if (mContentParent == null) { installDecor(); } else { mContentParent.removeAllViews(); } mContentParent.addView(view, params); final Callback cb = getCallback(); if (cb != null) { cb.onContentChanged(); //窗口类容发生变化时更新 } }
java代码:
java代码:
View decor =r.window.getDecorView(); //获得窗口的顶级View decor.setVisibility(View.INVISIBLE); ViewManager wm= a.getWindowManager(); //WindowManager继承自ViewManager WindowManager.LayoutParams l =r.window.getAttributes(); a.mDecor = decor; l.type =WindowManager.LayoutParams.TYPE_BASE_APPLICATION; l.softInputMode |= forwardBit; if (a.mVisibleFromClient) { a.mWindowAdded = true; wm.addView(decor, l); //实际上是把主窗口的顶级view加入到WindowMangaer }
WindowManager主要用来管理窗口的一些状态、属性、view增加、删除、更新、窗口顺序、消息收集和处理等。
通过Context.getSystemService(Context.WINDOW_SERVICE)的方式可以获得WindowManager的实例.
WindowManager继承自ViewManager,里面涉及到窗口管理的三个重要方法,分别是:
* addView();
* updateViewLayout();
* removeView();
在WindowManager中还有一个重要的静态类LayoutParams.通过它可以设置和获得当前窗口的一些属性。
我们先来看看addView()方法,在addView中,会利用LayoutParams获得window的View属性,并为每个window创建ViewRoot,ViewRoot是View和WindowManager之间的桥梁,真正把View传递给WindowManager的是通过ViewRoot的setView()方法,ViewRoot实现了View和WindowManager之间的消息传递。在将主窗口添加到WindowManger时,它首先会建立一个代理对象:
wm=(WindowManagerImpl)context.getSystemService(Context.WINDOW_SERVICE)
并且打开会话(IWindowSession),之后Window将通过该会话与WindowManager建立联系,
来看下setView方法:
java代码:
try { res =sWindowSession.add(mWindow, mWindowAttributes, getHostVisibility(), mAttachInfo.mContentInsets); } catch (RemoteException e) { mAdded = false; mView = null; mAttachInfo.mRootView =null; unscheduleTraversals(); throw newRuntimeException("Adding window failed", e); } finally { if (restore) { attrs.restore(); } }
http://wenzhutech.diandian.com/post/2011-09-03/4649021