Android : View.inflate 和 getLayoutInflater().inflate 有什么区别?
Android : What is the difference between View.inflate and getLayoutInflater().inflate?
真正的区别是什么:
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);
}
实际上在后端做同样的工作,你提到的第一种方法。
他们是一样的,做同样的事情
在View.java
class
public static View inflate(Context context, @LayoutRes int resource, ViewGroup root) {
LayoutInflater factory = LayoutInflater.from(context);
return factory.inflate(resource, root);
}
和 LayoutInflater.from(context)
return LayoutInflator
对象。这与调用 getLayoutInflator()
方法相同。
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;
}
真正的区别是什么:
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);
}
实际上在后端做同样的工作,你提到的第一种方法。
他们是一样的,做同样的事情
在View.java
class
public static View inflate(Context context, @LayoutRes int resource, ViewGroup root) {
LayoutInflater factory = LayoutInflater.from(context);
return factory.inflate(resource, root);
}
和 LayoutInflater.from(context)
return LayoutInflator
对象。这与调用 getLayoutInflator()
方法相同。
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;
}