當你需要參考LayoutInflater
Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
(ViewGroup.inflate
和Activity.getLayoutInflater
只是方便的包裝),我相信它總是返回參照同充氣服務,直到它被破壞並重新創建,然後這個新創建的返回,等on ...我認爲從getSystemService方法獲得的manager對象就像「普通」服務的binder對象一樣。
編輯: ,說上面我錯了;)
我檢查了源代碼,並在android.view.ContextThemeWrapper(這是活動中的超級類):
@Override public Object getSystemService(String name) {
if (LAYOUT_INFLATER_SERVICE.equals(name)) {
if (mInflater == null) {
mInflater = LayoutInflater.from(mBase).cloneInContext(this);
}
return mInflater;
}
return mBase.getSystemService(name);
}
和機器人.app.ContextImpl這可能是MBASE上下文實現:
public Object getSystemService(String name) {
ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name);
return fetcher == null ? null : fetcher.getService(this);
}
其中: 私有靜態最後的HashMap SYSTEM_SERVICE_MAP = new HashMap();
和ServiceFetcher是緩存和檢索「系統服務實例」的內部類。
確定每個activity/ContextWrapper有一個LayoutInflater。對於更多的觀察研究來源,請;)
**不幸的是,有些地方在我的應用程序中它不起作用。**什麼意思是「它不工作」?正如AedonEtLIRA在他們的回答中所說的那樣,發佈不起作用的代碼。 – Squonk