獲取LayoutInflater的方法

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);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章