为什么 Activity 没有 getContentView() 方法?
Activity
类有一个 setContentView()
方法。 PopupWindow
类有一个 getContentView()
方法,但没有其他方法。是否有其他方法来获取活动的主要内容视图?
The Activity
class has a setContentView()
method. The PopupWindow
Class has a getContentView()
method but nothing else does. Is there another way to get the main content view for an activity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我能够通过以下调用获取 Activity 的内容:
您可能应该检查 getDecorView 是否在所有情况下返回 ViewGroup 的实例,但在 Activity 中使用 LinearLayout 时,上面的代码运行良好。要访问 LinearLayout,您可以:
如果您有这样的函数:
您可以在 Activity 中使用以下调用迭代所有视图并记录它们的类名:
I was able to get to the contents of an Activity with this call:
You should probably check that getDecorView returns an instanceof ViewGroup for all cases, but with a LinearLayout in the Activity the code above runs fine. To get to the LinearLayout you could then just:
And if you have a function like this:
You could iterate through all views and log their class names with the following call inside your Activity:
下面这行就可以解决问题:
它本质上是相同的(它需要在 Activity 的上下文中调用)
Following line will do the trick:
it is essentially same as (it needs to be called on the context of an Activity)
我也在寻找这个,但我只是认为向最外面的 ViewGroup 添加 id 可能会更容易。
不过,我会继续寻找几分钟。我很喜欢它,这样我就可以使用 findViewWithTag 从最外面的布局。
I'm looking for this as well, but I just thought that it might be easier to add an id to the outermost ViewGroup.
I'll keep looking for a few more minutes, though. I'm into it so that I can use findViewWithTag from the outermost layout.