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

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

问题描述

我知道如何使用 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).getRootView()

据报道,在某些设备上,您必须使用

Also it was reported that on some devices you have to use

getWindow().getDecorView().findViewById(android.R.id.content)

相反.

请注意,正如 Booger 报道的那样,这在某些设备上可能位于导航栏(带有后退按钮等)后面(但在大多数设备上似乎并非如此).

Please note that as Booger reported, this may be behind navigation bar (with back button etc.) on some devices (but 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);

但最好在您的 xml 布局中将 id 设置为此视图并使用此 id 代替.

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

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

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