需要导入什么,或者我如何在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类中的地方使用充气装置。
我该怎么做呢?
发布于 2013-09-22 18:21:41
或者..。
LayoutInflater inflater = LayoutInflater.from(context);
发布于 2014-06-11 05:50:46
或
View.inflate(context, layout, parent)
发布于 2011-10-18 15:31:31
使用context对象,您可以从以下代码中获取LayoutInflater
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
https://stackoverflow.com/questions/7803771
复制相似问题