一、什么是LayoutInflater?
LayoutInflater是一个视图填充器;使用它来把布局转为一个View。
二、LayoutInflater的获取
第一种方式: 从给定的上下文中获取LayoutInflater:
LayoutInflater inflater = LayoutInflater.from(context);
第二种方式:在Activity中获取LayoutInflater:
LayoutInflater inflater = getLayoutInflater();
第三种方式:通过context.getSystemService()获取:
LayoutInflater inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater的from方法
先给出结论:我们通过LayoutInflater的from(Context context)方法获取一个LayoutInflater对象。传递不同的Context对象,获取到的LayoutInflater对象也不同。每一个Activity 都会持有一个 LayoutInflater 对象, 如果每次传递的Context对象都是同一个Activity 对象,只会创建一个 LayoutInflater 对象。