修改窗口的尺寸要在onAttachedToWindow后才能成功,在这之前,getWindow().getDecorView().getLayoutParams返回值是空的
DecorView的LayoutParams是在ActivityThread.handleResumeActivity中调用WindowManagerGlobal.addView进行设置的,
root = new ViewRootImpl(view.getContext(), display);
view.setLayoutParams(wparams);
mViews.add(view);
mRoots.add(root);
mParams.add(wparams);
// do this last because it fires off messages to start doing things
try {
root.setView(view, wparams, panelParentView);
} catch (RuntimeException e) {
// BadTokenException or InvalidDisplayException, clean up.
if (index >= 0) {
removeViewLocked(index, true);
}
throw e;
}
在这之后会在ViewRootImpl.setView中通过requestLayout触发performTraversals 最终回调到Activity.onAttachedToWindow
private void performTraversals() {
// cache mView since it is used so much below...
final View host = mView;
...
host.dispatchAttachedToWindow(mAttachInfo, 0);
...
}
调用流程
View.dispatchAttachedToWindow-->DecorView.onAttachedToWindow-->Activity.onAttachedToWindow