为什么 Activity 没有 getContentView() 方法?

发布于 2024-09-19 16:38:21 字数 159 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

够钟 2024-09-26 16:38:21

我能够通过以下调用获取 Activity 的内容:

ViewGroup view = (ViewGroup)getWindow().getDecorView();

您可能应该检查 getDecorView 是否在所有情况下返回 ViewGroup 的实例,但在 Activity 中使用 LinearLayout 时,上面的代码运行良好。要访问 LinearLayout,您可以:

LinearLayout content = (LinearLayout)view.getChildAt(0);

如果您有这样的函数:

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 + " ");
    }
}

您可以在 Activity 中使用以下调用迭代所有视图并记录它们的类名:

logContentView(getWindow().getDecorView(), "");

I was able to get to the contents of an Activity with this call:

ViewGroup view = (ViewGroup)getWindow().getDecorView();

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(), "");
苹果你个爱泡泡 2024-09-26 16:38:21

下面这行就可以解决问题:

findViewById(android.R.id.content);

它本质上是相同的(它需要在 Activity 的上下文中调用)

this.findViewById(android.R.id.content);

Following line will do the trick:

findViewById(android.R.id.content);

it is essentially same as (it needs to be called on the context of an Activity)

this.findViewById(android.R.id.content);
心欲静而疯不止 2024-09-26 16:38:21

我也在寻找这个,但我只是认为向最外面的 ViewGroup 添加 id 可能会更容易。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/outer">
    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">

不过,我会继续寻找几分钟。我很喜欢它,这样我就可以使用 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.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/outer">
    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">

I'll keep looking for a few more minutes, though. I'm into it so that I can use findViewWithTag from the outermost layout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文