在Android中获取布局inflater的正确方法是什么? [英] What is the correct way to get layout inflater in Android?

查看:215
本文介绍了在Android中获取布局inflater的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法可以获得layoutInflater:

  LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

另一种方式是:

  LayoutInflater inflater = LayoutInflater.from(context); 

第三个(当我在活动中时)是:

  LayoutInflater inflater = getLayoutInflater(); 

那么它们有什么区别?



<请注意,当我将第三个inflater发送到我的适配器时,我的应用程序工作正常。但是当我发送上下文并通过第二种方式创建了inflater时,它没有!

解决方案

没有多少它们之间存在差异。



正如文档所述 public abstract Object getSystemService(String name)


LayoutInflater用于在此上下文中扩展布局资源。


对于 public static LayoutInflater from(Context context)



< blockquote>

从给定的上下文中获取LayoutInflater。


您可以查看此主题 getLayoutInflater()和.getSystemService(Context.LAYOUT_INFLATER_SERVICE)之间是否存在差异


There is a way to get layoutInflater:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

and another way is:

LayoutInflater inflater = LayoutInflater.from(context);

a third one (when I am in an Activity) is:

LayoutInflater inflater = getLayoutInflater();

So what is the difference between them?

Note that when I sent the third inflater to my adapter, my application worked. But when I sent the context and created the inflater via the second way, it didn't!

解决方案

There is not much of a difference between them.

As doc says public abstract Object getSystemService (String name)

A LayoutInflater for inflating layout resources in this context.

And for the public static LayoutInflater from (Context context)

Obtains the LayoutInflater from the given context.

You can check this thread Is there any difference between getLayoutInflater() and .getSystemService(Context.LAYOUT_INFLATER_SERVICE)

这篇关于在Android中获取布局inflater的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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