作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
什么是真正的区别:
return context.getLayoutInflater().inflate(R.layout.my_layout, null);
Inflate a new view hierarchy from the specified xml resource.
和
return View.inflate(context, R.layout.my_layout, null);
Inflate a view from an XML resource. This convenience method wraps the LayoutInflater class, which provides a full range of options for view inflation.
最佳答案
两者是一样的。第二个版本只是完成任务的一种方便而简短的方法。如果您查看 View.inflate()
方法的源代码,您会发现:
/**
* Inflate a view from an XML resource. This convenience method wraps the {@link
* LayoutInflater} class, which provides a full range of options for view inflation.
*
* @param context The Context object for your activity or application.
* @param resource The resource ID to inflate
* @param root A view group that will be the parent. Used to properly inflate the
* layout_* parameters.
* @see LayoutInflater
*/
public static View inflate(Context context, int resource, ViewGroup root) {
LayoutInflater factory = LayoutInflater.from(context);
return factory.inflate(resource, root);
}
实际上在后端做同样的工作,你提到的第一种方法。
关于Android : What is the difference between View. 膨胀和 getLayoutInflater().膨胀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35548844/
我是一名优秀的程序员,十分优秀!