需要导入什么以及如何在非活动(activity)的其他地方调用布局膨胀器(Layout inflater)?
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();
}
我只能在活动中调用getLayoutInflater
,这是一种限制吗?如果我想创建自定义对话框并为其填充视图,或者如果我想要具有自定义视图的Toast消息,并且该消息是从服务显示的,我只有来自服务的上下文,没有任何活动,但我想显示自定义消息。
我需要在代码中不在活动类中的位置使用inflater。
我该如何做到这一点?
getSystemService
的调用由于某种(不太可能的)原因未返回LayoutInflater
类型的对象,则我的代码将导致运行时异常。 - kaspermoerch