三種inflate方式的區別

2021-09-12 22:16:09 字數 1195 閱讀 4589

在安卓開發中,經常要將乙個xml布局檔案inflate成乙個view物件,用到的方法通常是三種:

(1)view.inflate(context context, int resource, viewgroup root);

(2)layoutinflater.from(context).inflate(int resource, viewgroup root);

(3)getsystemservice(context.layout_inflater_service);

那麼這三種inflate之間有什麼區別和聯絡呢?

通過原始碼其實很容易知道這三種之間有什麼關聯,其實很簡單,view.inflate內部呼叫layoutinflater.from(context).inflate,然後layoutinflate內部呼叫getsystemservice(context.layout_inflater_service)得到layoutinflater例項,說到底最終都是拿到系統的inflater.

public static view inflate(context context, int resource, viewgroup root)
public view inflate(int resource, viewgroup root) 

public view inflate(xmlpullparser parser, viewgroup root)

public view inflate(int resource, viewgroup root, boolean attachtoroot) finally

}public view inflate(xmlpullparser parser, viewgroup root, boolean attachtoroot)

layoutinflater 中有許多的inflate方法,但最終都是呼叫到最下面的乙個方法.這個方法有兩個很重要的引數viewgroup root和boolean attachtoroot,如果通過view.inflate方法呼叫到這個方法那麼這兩個引數要麼乙個為root為null、attachtoroot為false;要麼root不為null、attachtoroot為true.但直接通過layoutinflater呼叫這個方法的話,引數就可以任意設定,一旦設定不當,就會出現很奇怪的現象。具體引數的使用情況見:layoutinflater引數的使用

三種繼承方式的區別

class parent public int m npub protected int m nptd private int m nprt class child1 public parent int getptd int gerprt a class child2 protected paren...

三種繼承方式的區別

公有繼承 基類成員在派生類中的訪問許可權保持不變,也就是說,基類中的成員訪問許可權,在派生類中仍然保持原來的訪問許可權 基類成員訪問屬性 派生類成員訪問屬性 private成員 無法訪問 protected成員 protected public成員 public 私有繼承 基類所有成員在派生類中的訪...

ISP ICP IAP三種程式設計方式區別總結

本帖最後由 xpzheng888 於 2011 12 27 13 17 編輯 最近在整理客戶的一些疑問,給自己充充電,也跟大家一起學習下。isp icp iap三種程式設計方式區別 isp in system programing,在系統程式設計 icp in circuit programing,...