LayoutInflater作用是將layout的xml佈局文件實例化爲View類對象。
獲取LayoutInflater的方法有如下三種:
1.
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.main, null);
2.
LayoutInflater inflater = LayoutInflater.from(context); (該方法實質就是第一種方法,可參考源代碼)
View layout = inflater.inflate(R.layout.main, null);
3.
LayoutInflater inflater = getLayoutInflater();(在Activity中可以使用,實際上是View子類下window的一個函數)
View layout = inflater.inflate(R.layout.main, null);