LayoutInflater的inflate函數用法詳解
LayoutInflater作用是將layout的xml布局文件實例化為View類對象。</p> 獲取LayoutInflater的方法有如下三種:
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.main, null ); LayoutInflater inflater = LayoutInflater.from(context); (該方法實質就是第一種方法,可參考源代碼) View layout = inflater.inflate(R.layout.main, null ); LayoutInflater inflater = getLayoutInflater();(在Activity中可以使用,實際上是View子類下window的一個函數) View layout = inflater.inflate(R.layout.main, null ); 一直有點糾結setContentView和inflate的區別找了一些資料。寫了個小程序看了下:
| |