在一个活动中,你可以做
View rootView = null;
View currentFocus = getWindow().getCurrentFocus();
if (currentFocus != null)
rootView = currentFocus.getRootView();
如上所述,还有
View decorView = getWindow().getDecorView();
也
View decorView = getWindow().peekDecorView();
The difference between the latter two is that peekDecorView()
may return null
if the decor view has not been created yet, whereas getDecorView()
will create a new decor view if none exists (yet). The first example may also return null
if no view currently has focus.
I haven't tried out whether the root view and the decor view are the same instance. Based on the documentation, though, I would assume they are, and it could be easily verified with a few lines of code.