Ham*_*boh 30 java android layout-inflater
有一种方法可以获得layoutInflater:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Run Code Online (Sandbox Code Playgroud)
另一种方式是:
LayoutInflater inflater = LayoutInflater.from(context);
Run Code Online (Sandbox Code Playgroud)
第三个(当我参加活动时)是:
LayoutInflater inflater = getLayoutInflater();
Run Code Online (Sandbox Code Playgroud)
那么他们之间有什么区别?
请注意,当我将第三个inflater发送到我的适配器时,我的应用程序工作.但是当我发送上下文并通过第二种方式创建了inflater时,它没有!
sac*_*n10 18
在您的活动之外使用
LayoutInflater inflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE );
Run Code Online (Sandbox Code Playgroud)
在你的活动中
LayoutInflater inflater = getLayoutInflater();
Run Code Online (Sandbox Code Playgroud)
如果您打开Android源代码,您可以看到LayoutInflator.from方法如下所示:
/**
* Obtains the LayoutInflater from the given context.
*/
public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater == null) {
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}
Run Code Online (Sandbox Code Playgroud)
而且没有区别
只要调用的Activity或Window getLayoutInflater()具有可以调用的相同Context,就getSystemService()没有区别.
小智 6
它们之间没有太大区别.
正如doc所说公共抽象Object getSystemService(String name)
LayoutInflater用于在此上下文中展开布局资源.
而对于公共静态LayoutInflater来自(Context context)
从给定的上下文中获取LayoutInflater.
你可以查看这个帖子getLayoutInflater()和.getSystemService(Context.LAYOUT_INFLATER_SERVICE)之间有什么区别吗
唯一的区别是您使用的上下文。如果您用于LayoutInflater.fromContext()或的上下文context.getSystemService(...)实际上是一个Activity,则它应等效于Activity.getLayoutInflater()。如果是应用程序对象,则可能会膨胀包含IIRC片段的视图。
| 归档时间: |
|
| 查看次数: |
21552 次 |
| 最近记录: |