一、首先我们先看下Activity是如何显示View
平常我们在Activity的onCreate会调用setContentView(R.layout.xxx),Activity启动过程与window的源码流程可参考
总结几个步骤:
1、创建Activity:ActivityThread的performLaunchActivity函数中创建Activity后,调用Activity.attach函数
2、创建PhoneWindow:Activity.attach函数中创建与之关联的PhoneWindow,PhoneWindow会创建DectorView。
3、添加视图:setContentView添加到PhoneWindow的DectorView中
4、关联PhoneWindow到WMS中:ActivityThread实行完performLaunchActivity,会handleResumeActivity,走到Activity的onResume,
然后设置Activity的PhoneWindow的type类型:
r.window = r.activity.getWindow();
View decor = r.window.getDecorView();
decor.setVisibility(View.INVISIBLE);
ViewManager wm = a.getWindowManager();
WindowManager.LayoutParams l = r.window.getAttributes();
a.mDecor = decor;
l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;//
l.softInputMode |= forwardBit;
,再把这个DectorView通过WindowManagerImpl-àWindowManagerGlobalàViewRootImplà(binder)WMS中的Session的addToDisplay.
我们看下l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
WindowManager.java:
/**
* Window type: an application window that serves as the "base" window
* of the overall application; all other application windows will
* appear on top of it.
* In multiuser systems shows only on the owning user's window.
*/
public static final int TYPE_BASE_APPLICATION = 1;
应用程序的window
二、Dialog显示View:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Message部分");
builder.setTitle("Title部分");
builder.setView(R.layout.dialog_main);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,<