问题描述
在活动
类有一个的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?
推荐答案
我能得到一个活动的这个电话的内容:
I was able to get to the contents of an Activity with this call:
ViewGroup view = (ViewGroup)getWindow().getDecorView();
您也许应该检查getDecorView返回一个instanceof的ViewGroup适用于所有情况,但与LinearLayout中活动中的code以上运行良好。要到达的LinearLayout然后你可以只是:
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:
LinearLayout content = (LinearLayout)view.getChildAt(0);
如果你有一个函数是这样的:
And if you have a function like this:
void logContentView(View parent, String indent) {
Log.i("test", indent + parent.getClass().getName());
if (parent instanceof ViewGroup) {
ViewGroup group = (ViewGroup)parent;
for (int i = 0; i < group.getChildCount(); i++)
logContentView(group.getChildAt(i), indent + " ");
}
}
您可以遍历所有的意见,并与您的活动里面以下通话记录自己的类名:
You could iterate through all views and log their class names with the following call inside your Activity:
logContentView(getWindow().getDecorView(), "");
这篇关于为什么没有一个getContentView()方法的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!