Android 如何从使用 setContentView 设置的布局中获取根 ViewGroup?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24612364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get root ViewGroup from a layout that was set using setContentView?
提问by mskw
I cannot seem to find a way to get this ViewGroup. Not sure if there are any standard ways to get ViewGroups.
我似乎无法找到获取此 ViewGroup 的方法。不确定是否有任何标准方法来获取 ViewGroup。
What I want to accomplish is to dynamically add a full screen overlay view to any Layout at run time, and in order to do this, I always need to get the root ViewGroup. Is this the right way to do this?
我想要完成的是在运行时向任何布局动态添加全屏覆盖视图,为了做到这一点,我总是需要获取根视图组。这是正确的方法吗?
回答by vRallev
Try using
尝试使用
ViewGroup view = (ViewGroup) findViewById(android.R.id.content);
Every layout has this ViewGroup as parent.
每个布局都将此 ViewGroup 作为父级。
回答by Yogesh Rathi
getWindow().getDecorView().getRootView();
set this view directly
直接设置这个视图
回答by Sergey Shustikov
Works for me
为我工作
getWindow().getDecorView().findViewById(android.R.id.content);