Android LayoutInflater

LayoutInflater簡介

LayoutInflater是一個抽象類,PhoneLayoutInflater繼承於它。java

LayoutInflater加載過程

一、經過getSystemServive()或LayoutInflater.from()獲取一個LayoutInflater實例(實際獲取的是PhoneLayoutInflater實例)。android

二、layoutInflater.inflater()中解析xml的過程就是分析XML節點,主要由requestFocus、merge、include和普通節點(每種節點都有本身的處理方法)。在PhoneLayoutInflater中有一個數組,是一些package name的前綴,有web

private static final String[] sClassPrefixList = {
    "android.widget.",
    "android.webkit.",
    "android.app."
};

三、onCreateView方法經過解析到的Node Name和前綴拼接,若是能找到該類則建立一個實例並添加到建立的View中。數組

四、LayoutInflater.inflater(int layoutResID, ViewGroup root, bool attachToRoot)經過解析layout xml獲得view,根據attachToRoot決定是否將獲得的View添加到root中。app

PS:若是root爲null,默認不添加到root中。spa

使用場景

場景1:code

Activity的setContentView(int layoutID)。若是Activity繼承了FragmentActivity或Activity,最終會調用PhoneWindow中的setContentView()。setContentView(View view)會調用layoutInflater.inflater()解析出界面並經過addView(view)到父view中。xml

場景2:繼承

ListView Adapter中的getItem,利用layoutInflater.inflate()直接獲取界面。這裏的rootView賦值爲null。get

相關文章
相關標籤/搜索