获取LayoutInflater有三种不同的方式,那么这三种方式有什么区别呢?
源码:
① LayoutInflater inflater = LayoutInflater.from(context); (LayoutInflater类)
<span style="font-size:14px;">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;
}</span><span style="font-size:14px;color:#330033;">public LayoutInflater getLayoutInflater() {
return getWindow().getLayoutInflater();
}</span>
public PhoneWindow(Context context) {
super(context);
mLayoutInflater = LayoutInflater.from(context);
}
<span style="font-size:14px;color:#330033;"> public static final String LAYOUT_INFLATER_SERVICE = "layout_inflater";</span>ps: Use with
getSystemService(java.lang.String) to
retrieve a android.view.LayoutInflater for
inflating layout resources in this context.
从源码上,三种方式在本质都是一样的,②是调用①的方法,而①则是在调用③。java很有意思就是这样子调来调去,给人一种似是而非的感觉。
对于getSystemService类,我们在源码中可以看到提供了很多的Service:
未完待续
原文地址:http://blog.csdn.net/violetic/article/details/44980757