从活动外调用findViewById() [英] Calling findViewById() from outside an activity

查看:92
本文介绍了从活动外调用findViewById()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从非活动的派生类访问布局的看法?我创建了手风琴类和需要访问一些活动的UI元素。我传递活动的背景下,以我的手风琴类的构造函数,但findViewById API只能从活动类。我也不想通过我的活动的一个实​​例,因为这似乎在因潜在的内存泄漏被皱起了眉头。

Is there any way to access a layout's view from a non-Activity-derived class? I'm creating an Accordion class and need to access some of the activity's UI elements. I'm passing in the activity's context to my accordion class's constructor, but the findViewById API is only available from the Activity class. I also don't want to pass in an instance of my activity since that seems to be frowned upon due to potential memory leaks.

推荐答案

我通过在浏览到类的构造函数中的一个实例。

I passed in an instance of one of the Views into the class's constructor.

这篇关于从活动外调用findViewById()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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