上一篇LayoutInflater源碼分析(一)我們分析了LayoutInflater的from()方法咪辱,這節(jié)我們來分析一下inflate()方法嘉冒。
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_vip, parent, false);
最終都會進(jìn)入到如下代碼:
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
synchronized (mConstructorArgs) {
Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");
final Context inflaterContext = mContext;
final AttributeSet attrs = Xml.asAttributeSet(parser);
Context lastContext = (Context) mConstructorArgs[0];
mConstructorArgs[0] = inflaterContext;
View result = root;
//令人窒息操作part one~
try {
// Look for the root node.
int type;
while ((type = parser.next()) != XmlPullParser.START_TAG &&
type != XmlPullParser.END_DOCUMENT) {
// Empty
}
if (type != XmlPullParser.START_TAG) {
throw new InflateException(parser.getPositionDescription()
+ ": No start tag found!");
}
final String name = parser.getName();
//去掉部分代碼
//令人窒息操作part two ~
if (TAG_MERGE.equals(name)) {
if (root == null || !attachToRoot) {
throw new InflateException("<merge /> can be used only with a valid "
+ "ViewGroup root and attachToRoot=true");
}
rInflate(parser, root, inflaterContext, attrs, false);
} else {
//令人窒息操作part three ~
// Temp is the root view that was found in the xml
final View temp = createViewFromTag(root, name, inflaterContext, attrs);
ViewGroup.LayoutParams params = null;
if (root != null) {
if (DEBUG) {
System.out.println("Creating params from root: " +
root);
}
// Create layout params that match root, if supplied
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {
// Set the layout params for temp if we are not
// attaching. (If we are, we use addView, below)
temp.setLayoutParams(params);
}
}
if (DEBUG) {
System.out.println("-----> start inflating children");
}
// Inflate all children under temp against its context.
rInflateChildren(parser, temp, attrs, true);
if (DEBUG) {
System.out.println("-----> done inflating children");
}
// We are supposed to attach all the views we found (int temp)
// to root. Do that now.
if (root != null && attachToRoot) {
root.addView(temp, params);
}
// Decide whether to return the root that was passed in or the
// top view found in xml.
if (root == null || !attachToRoot) {
result = temp;
}
}
} catch (XmlPullParserException e) {
final InflateException ie = new InflateException(e.getMessage(), e);
ie.setStackTrace(EMPTY_STACK_TRACE);
throw ie;
} catch (Exception e) {
final InflateException ie = new InflateException(parser.getPositionDescription()
+ ": " + e.getMessage(), e);
ie.setStackTrace(EMPTY_STACK_TRACE);
throw ie;
} finally {
// Don't retain static reference on context.
mConstructorArgs[0] = lastContext;
mConstructorArgs[1] = null;
Trace.traceEnd(Trace.TRACE_TAG_VIEW);
}
return result;
}
}
通過代碼和注釋我們可以看到,拿到XmlResourceParser parser是用于節(jié)點(diǎn)的解析丐枉。
比如part one 從頭到尾遍歷xml文件的標(biāo)簽,直到
文檔結(jié)束才跳出循環(huán)。如果該xml沒有開始標(biāo)簽谍倦,則拋異常。
part two 講的是什么呢泪勒?TAG_MERGE="merge"昼蛀,如果讀到的標(biāo)簽是merge,判斷是否有父View圆存,沒有則拋異常叼旋,有則跳轉(zhuǎn)到rInflate()解析merge的xml。
part three就是當(dāng)前的標(biāo)簽沒有其他子xml沦辙,要直接解析啦送淆。關(guān)鍵方法就是createViewFromTag()方法了。
(一)rInflate()解析
void rInflate(XmlPullParser parser, View parent, Context context,
AttributeSet attrs, boolean finishInflate) throws XmlPullParserException, IOException {
//獲取該xml的深度 ~~
final int depth = parser.getDepth();
int type;
while (((type = parser.next()) != XmlPullParser.END_TAG ||
parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
if (type != XmlPullParser.START_TAG) {
continue;
}
final String name = parser.getName();
//判斷標(biāo)簽是否為requestFocus[1]怕轿。requestFocus標(biāo)簽于指定屏幕內(nèi)的焦點(diǎn)View
if (TAG_REQUEST_FOCUS.equals(name)) {
parseRequestFocus(parser, parent);
//判斷標(biāo)簽是否為tag偷崩。
//設(shè)置一個文本標(biāo)簽∽灿穑可以通過View.getTag()或 for with View.findViewWithTag()檢索含有該標(biāo)簽字符串的View阐斜。
//但一般最好通過ID來查詢View,因為它的速度更快诀紊,并且允許編譯時類型檢查谒出。
} else if (TAG_TAG.equals(name)) {
parseViewTag(parser, parent, attrs);
//判斷標(biāo)簽是否為include。
} else if (TAG_INCLUDE.equals(name)) {
if (parser.getDepth() == 0) {
throw new InflateException("<include /> cannot be the root element");
}
parseInclude(parser, context, parent, attrs);
//判斷標(biāo)簽是否為merge。如果是笤喳,則直接拋出異常为居,因為Merge必須為根元素,也就是深度為0的節(jié)點(diǎn)杀狡。
} else if (TAG_MERGE.equals(name)) {
throw new InflateException("<merge /> must be the root element");
} else {
//沒啥奇奇怪怪標(biāo)簽了蒙畴。又出現(xiàn)了這個createViewFromTag()方法。這個方法其實(shí)就是根據(jù)標(biāo)簽(節(jié)點(diǎn)名)稱創(chuàng)建View呜象。
final View view = createViewFromTag(parent, name, context, attrs);
final ViewGroup viewGroup = (ViewGroup) parent;
final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
//加載標(biāo)簽的內(nèi)子類
rInflateChildren(parser, view, attrs, true);
//將該view添加進(jìn)Parent布局
viewGroup.addView(view, params);
}
}
//通知父View膳凝,解析完成。
if (finishInflate) {
parent.onFinishInflate();
}
}
我們點(diǎn)擊進(jìn)入rInflateChildren()方法恭陡,發(fā)現(xiàn)其實(shí)也是調(diào)用的rInflate()方法:
final void rInflateChildren(XmlPullParser parser, View parent, AttributeSet attrs,
boolean finishInflate) throws XmlPullParserException, IOException {
rInflate(parser, parent, parent.getContext(), attrs, finishInflate);
}
(二)createViewFromTag()解析
View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
boolean ignoreThemeAttr) {
//解析view標(biāo)簽蹬音,注意哦,是view不是View休玩,這個標(biāo)簽一般大家不太常用著淆。[2]
if (name.equals("view")) {
name = attrs.getAttributeValue(null, "class");
}
//如果需要該標(biāo)簽與主題相關(guān),需要對context進(jìn)行包裝拴疤,將主題信息加入context包裝類ContextWrapper
//好吧其實(shí)我不知道這個是什么鬼永部,哈哈哈
if (!ignoreThemeAttr) {
final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME);
final int themeResId = ta.getResourceId(0, 0);
if (themeResId != 0) {
context = new ContextThemeWrapper(context, themeResId);
}
ta.recycle();
}
//TAG_1995="blink"
if (name.equals(TAG_1995)) {
//BlinkLayou繼承自FrameLayout,它包裹的內(nèi)容會一直閃爍遥赚。[3]
return new BlinkLayout(context, attrs);
}
try {
View view;
if (mFactory2 != null) {
view = mFactory2.onCreateView(parent, name, context, attrs);
} else if (mFactory != null) {
view = mFactory.onCreateView(name, context, attrs);
} else {
view = null;
}
if (view == null && mPrivateFactory != null) {
view = mPrivateFactory.onCreateView(parent, name, context, attrs);
}
if (view == null) {
final Object lastContext = mConstructorArgs[0];
mConstructorArgs[0] = context;
try {
//indexOf()的用法:
//返回字符中indexof(string)中字串string在父串中首次出現(xiàn)的位置扬舒,從0開始!沒有返回-1凫佛。
//下面判斷語句是對自定義View和原生的控件進(jìn)行判斷讲坎。如果name中包含.即為自定義View,否則為原生的View控件
//例如:<Button>
if (-1 == name.indexOf('.')) {
view = onCreateView(parent, name, attrs);
} else {
//自定義控件愧薛,例如: <com.xxx.xxx.MyView>
view = createView(name, null, attrs);
}
} finally {
mConstructorArgs[0] = lastContext;
}
}
return view;
} catch (InflateException e) {
throw e;
} catch (ClassNotFoundException e) {
final InflateException ie = new InflateException(attrs.getPositionDescription()
+ ": Error inflating class " + name, e);
ie.setStackTrace(EMPTY_STACK_TRACE);
throw ie;
} catch (Exception e) {
final InflateException ie = new InflateException(attrs.getPositionDescription()
+ ": Error inflating class " + name, e);
ie.setStackTrace(EMPTY_STACK_TRACE);
throw ie;
}
}
[2]view相當(dāng)于所有控件標(biāo)簽的父類一樣晨炕,可以設(shè)置class屬性,這個屬性會決定view這個節(jié)點(diǎn)會變成什么控件毫炉。
<view
class="RelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"></view>
[3]blink這個標(biāo)簽很有好玩瓮栗,大家可以自己試試下面的代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<blink
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="一閃一閃亮晶晶"
android:textSize="18sp"
android:textColor="@android:color/holo_orange_light"
/>
</blink>
</LinearLayout>
我們看到mFactory 和mFactory2 ,他們的類型是Factory和Factory2瞄勾。而實(shí)際上Factory和Factory2都是一個接口费奸,需要自己實(shí)現(xiàn),并且Factory2繼承自Factory进陡,從而擴(kuò)展出一個參數(shù)愿阐,就是增加了該節(jié)點(diǎn)的父View。
public interface Factory2 extends Factory {
public View onCreateView(View parent, String name, Context context, AttributeSet attrs);
}
那么這兩個Factory是用來干嘛的呢趾疚?Factory和Factory2其實(shí)LayoutInflater解析View時的一種擴(kuò)展實(shí)現(xiàn)缨历,可以額外的對View處理以蕴,設(shè)置Factory和Factory2需要通過setFactory()或者setFactory2()來實(shí)現(xiàn)。
有沒有個具體例子演示一下辛孵?鴻洋大神給過一個例子丛肮,我稍作修改了代碼。xml中有一個TextView魄缚,但是經(jīng)過下面代碼修改宝与,將TextView變成了Button:
public class MainActivity extends AppCompatActivity
{
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState)
{
LayoutInflaterCompat.setFactory(LayoutInflater.from(this), new LayoutInflaterFactory()
{
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs)
{
//這這這,小哥哥我在這~
if (name.equals("TextView"))
{
Button button = new Button(context, attrs);
return button;
}
}
return null;
}
});
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
具體查看:Android 探究 LayoutInflater setFactory
需要強(qiáng)調(diào)一點(diǎn)鲜滩,LayoutInflater內(nèi)部定義了一個boolean類型的mFactorySet開關(guān)伴鳖,其值默認(rèn)值為false节值,當(dāng)我們調(diào)用過setFactory()或者是setFactory2()后mFactorySet為true徙硅,若我們再次調(diào)用這倆方法時會拋出異常,也就是說每一個LayoutInflater實(shí)例對象只能賦值一次Factory搞疗,若再想賦成其他值只能通過反射先把mFactorySet的值置為false防止拋異常嗓蘑。具體的大家可以去看setFactory()和setFactory2()代碼,我這邊不敘述了匿乃。
我們前面代碼標(biāo)注過桩皿,如果是原生控件,那它走:
view = onCreateView(parent, name, attrs);
我們點(diǎn)擊方法看一下:
protected View onCreateView(View parent, String name, AttributeSet attrs)
throws ClassNotFoundException {
return onCreateView(name, attrs);
}
再點(diǎn)進(jìn)去onCreateView()看下:
protected View onCreateView(String name, AttributeSet attrs)
throws ClassNotFoundException {
return createView(name, "android.view.", attrs);
}
我們可以看到標(biāo)簽名自動幫我們加上了"android.view"幢炸,再點(diǎn)進(jìn)createView()方法泄隔,發(fā)現(xiàn)最終該原生控件走的是和自定義控件一樣的createView()方法。這個方法有點(diǎn)長宛徊,大家堅持一會兒佛嬉,本章快結(jié)束了。
public final View createView(String name, String prefix, AttributeSet attrs)
throws ClassNotFoundException, InflateException {
// 以View的name為key, 查詢構(gòu)造函數(shù)的緩存map中是否已經(jīng)存在該View的構(gòu)造函數(shù).
Constructor<? extends View> constructor = sConstructorMap.get(name);
//verifyClassLoader()判斷ClassLoader是否安全
if (constructor != null && !verifyClassLoader(constructor)) {
constructor = null;
sConstructorMap.remove(name);
}
Class<? extends View> clazz = null;
try {
Trace.traceBegin(Trace.TRACE_TAG_VIEW, name);
// 找不到緩存的構(gòu)造方法
if (constructor == null) {
// 如果傳入的第二個參數(shù)prefix為null闸天,就說明name是完整的包名暖呕,是自定義控件,直接反射加載自定義View苞氮。
//如果第二個參數(shù)不為 null湾揽,就由前綴+name組成完整的類名。
clazz = mContext.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
// 如果有自定義的過濾器并且加載到字節(jié)碼,則通過過濾器判斷是否允許加載該View
if (mFilter != null && clazz != null) {
boolean allowed = mFilter.onLoadClass(clazz);
if (!allowed) {
failNotAllowed(name, prefix, attrs);
}
}
// 得到構(gòu)造函數(shù)
constructor = clazz.getConstructor(mConstructorSignature);
constructor.setAccessible(true);
// 緩存構(gòu)造函數(shù)
sConstructorMap.put(name, constructor);
} else {
if (mFilter != null) {
// 過濾的map是否包含了此類名
Boolean allowedState = mFilterMap.get(name);
if (allowedState == null) {
//加載Class對象操作
// New class -- remember whether it is allowed
clazz = mContext.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
//判斷Class是否可被加載
boolean allowed = clazz != null && mFilter.onLoadClass(clazz);
mFilterMap.put(name, allowed);
if (!allowed) {
failNotAllowed(name, prefix, attrs);
}
} else if (allowedState.equals(Boolean.FALSE)) {
failNotAllowed(name, prefix, attrs);
}
}
}
Object[] args = mConstructorArgs;
args[1] = attrs;
//如果過濾器不存在笼吟,直接實(shí)例化該View
final View view = constructor.newInstance(args);
//如果View屬于ViewStub那么需要給ViewStub設(shè)置一個克隆過的LayoutInflater
if (view instanceof ViewStub) {
// Use the same context when inflating ViewStub later.
final ViewStub viewStub = (ViewStub) view;
viewStub.setLayoutInflater(cloneInContext((Context) args[0]));
}
return view;
} catch (NoSuchMethodException e) {
final InflateException ie = new InflateException(attrs.getPositionDescription()
+ ": Error inflating class " + (prefix != null ? (prefix + name) : name), e);
ie.setStackTrace(EMPTY_STACK_TRACE);
throw ie;
} catch (ClassCastException e) {
// If loaded class is not a View subclass
final InflateException ie = new InflateException(attrs.getPositionDescription()
+ ": Class is not a View " + (prefix != null ? (prefix + name) : name), e);
ie.setStackTrace(EMPTY_STACK_TRACE);
throw ie;
} catch (ClassNotFoundException e) {
// If loadClass fails, we should propagate the exception.
throw e;
} catch (Exception e) {
final InflateException ie = new InflateException(
attrs.getPositionDescription() + ": Error inflating class "
+ (clazz == null ? "<unknown>" : clazz.getName()), e);
ie.setStackTrace(EMPTY_STACK_TRACE);
throw ie;
} finally {
Trace.traceEnd(Trace.TRACE_TAG_VIEW);
}
}
具體的注釋我寫在代碼里了库物,大家自行查閱。希望看完文章贷帮,大家花個兩分鐘思考一下LayoutInflate的一些主要方法的作用是什么戚揭、xml是怎么轉(zhuǎn)變成view的。有機(jī)會我會再開一篇文章皿桑,講講LayoutInflater的一點(diǎn)實(shí)戰(zhàn)內(nèi)容毫目。
感謝:Android 中LayoutInflater(布局加載器)系列博文說明等網(wǎng)絡(luò)各種博客蔬啡。歡迎糾錯,互相學(xué)習(xí)~
[1]
<EditText
android:id="@+id/et_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number">
<requestFocus />
</EditText>