获得从当前活动的根视图 [英] Get root view from current activity

查看:173
本文介绍了获得从当前活动的根视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道怎么去用根视图<一href="http://developer.android.com/reference/android/view/View.html#getRootView%28%29">View.getRootView().我也能够从一个按钮的的onClick 事件的看法,其中一个参数是一个的视图。但我怎么能获得查看活动

I know how to get the root view with View.getRootView(). I am also able to get the view from a button's onClick event where the argument is a View. But how can I get the view in an activity?

推荐答案

如果你需要你的活动的根视图(这样你就可以有添加内容)使用

If you need root view of your activity (so you can add your contents there) use

findViewById(android.R.id.content)

请注意,由于布格报道,这可能是背后的导航栏(与后退按钮等)在某些设备(它似乎在大多数设备是不是)。

please note that as Booger reported, this may be behind navigation bar (with back button etc.) on some devices (and it seems on most devices it is not).

如果你需要,你使用的setContentView()方法,然后添加到您的活动作为pottedmeat写了,你可以用它来获得视图

If you need to get view that you added to your activity using setContentView() method then as pottedmeat wrote you can use

final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
            .findViewById(android.R.id.content)).getChildAt(0);

不过还好刚才设置ID这一观点在XML的布局,并使用这个ID代替。

But better just set id to this view in your xml layout and use this id instead.

这篇关于获得从当前活动的根视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆