1.Context的获取
一般我们用的是××Activity.this。其实还有另一种方式,从当前显示的某个view中获取,也就是调用view.getContext()方法。2.LayoutInflater的获取
第一种方式:
LayoutInflater inflater = LayoutInflater.from(××Activity.this); from()方法api里是这样说的 Obtains the LayoutInflater from the given context.从给定的Context中获取LayoutInflater。第二种方式:
调用Activity.getLayoutInflater()获得。我们来看API里是怎么说的: Convenience for callingWindow.getLayoutInflater()
.继续点进去后有这样一句说明-- Quick access to theLayoutInflater
instance that this Window retrieved from its Context.翻译一下,也就是说快速使用这个LayoutInflater实例,该实例是window从它的Context取到的。第三种方式:
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 这个也就是从系统服务中获得LayoutInflater。3.LayoutInflater的用法:
View layout = inflater.inflate(R.layout.main, null); 注:系统加载xml,同样用的是LayoutInflater
LayoutInflater结合Context使用
最新推荐文章于 2021-11-20 13:41:51 发布