我可以通过以下方式获取LayoutInflater:
inflater = LayoutInflater.from(context);
或者
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
哪种方式更快且更受推荐?
我可以通过以下方式获取LayoutInflater:
inflater = LayoutInflater.from(context);
或者
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
/**
* 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;
}
long startTime = System.nanoTime();
inflater = LayoutInflater.from(context);
long endTime = System.nanoTime();
long duration = (endTime - startTime); //divide by 1000000 to get milliseconds.
long startTime = System.nanoTime();
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
long endTime = System.nanoTime();
long duration = (endTime - startTime); //divide by 1000000 to get milliseconds.
LayoutInflater
才会有所影响呢?顺便说一下,这里建议使用第二种方法。 - ci_