首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在非活动位置调用getLayoutInflater()

在非活动位置调用getLayoutInflater()
EN

Stack Overflow用户
提问于 2011-10-18 15:23:52
回答 5查看 158.2K关注 0票数 201

需要导入什么,或者我如何在activity以外的地方调用布局充气?

public static void method(Context context){
    //this doesn't work the getLayoutInflater method could not be found
    LayoutInflater inflater = getLayoutInflater();
    // this also doesn't work 
    LayoutInflater inflater = context.getLayoutInflater();
}

我只能在activity中调用getLayoutInflater,这是一个限制吗?如果我想要创建自定义对话框,并想要为其扩展视图,或者如果我想让Toast message具有自定义视图,并从服务中显示,我只有来自服务上下文,我没有任何活动,但我想显示自定义消息,该怎么办?

我需要在代码中不在activity类中的地方使用充气装置。

我该怎么做呢?

EN

回答 5

Stack Overflow用户

发布于 2013-09-22 18:21:41

或者..。

LayoutInflater inflater = LayoutInflater.from(context);
票数 281
EN

Stack Overflow用户

发布于 2014-06-11 05:50:46

View.inflate(context, layout, parent)

票数 11
EN

Stack Overflow用户

发布于 2011-10-18 15:31:31

使用context对象,您可以从以下代码中获取LayoutInflater

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7803771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档