设为首页 加入收藏

TOP

Android LayoutInflater源码解析(二)
2015-08-31 21:23:16 来源: 作者: 【 】 浏览:65
Tags:Android LayoutInflater 源码 解析
ws 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) {
? ? ? ? ? ? ? ? InflateException ex = new InflateException(e.getMessage());
? ? ? ? ? ? ? ? ex.initCause(e);
? ? ? ? ? ? ? ? throw ex;
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? InflateException ex = new InflateException(
? ? ? ? ? ? ? ? ? ? ? ? parser.getPositionDescription()
? ? ? ? ? ? ? ? ? ? ? ? + ": " + e.getMessage());
? ? ? ? ? ? ? ? ex.initCause(e);
? ? ? ? ? ? ? ? throw ex;
? ? ? ? ? ? } finally {
? ? ? ? ? ? ? ? // Don't retain static reference on context.
? ? ? ? ? ? ? ? mConstructorArgs[0] = lastContext;
? ? ? ? ? ? ? ? mConstructorArgs[1] = null;
? ? ? ? ? ? }


? ? ? ? ? ? return result;
? ? ? ? }
? ? }


这里,Android使用了PULL来解析xml布局文件,并通过反射来创建出当前view:


temp = createViewFromTag(root, name, attrs);


我们查看一下源码:


View createViewFromTag(View parent, String name, AttributeSet attrs) {
? ? ? ? if (name.equals("view")) {
? ? ? ? ? ? name = attrs.getAttributeva lue(null, "class");
? ? ? ? }


? ? ? ? if (DEBUG) System.out.println("******** Creating view: " + name);


? ? ? ? try {
? ? ? ? ? ? View view;
? ? ? ? ? ? if (mFactory2 != null) view = mFactory2.onCreateView(parent, name, mContext, attrs);
? ? ? ? ? ? else if (mFactory != null) view = mFactory.onCreateView(name, mContext, attrs);
? ? ? ? ? ? else view = null;


? ? ? ? ? ? if (view == null && mPrivateFactory != null) {
? ? ? ? ? ? ? ? view = mPrivateFactory.onCreateView(parent, name, mContext, attrs);
? ? ? ? ? ? }
? ? ? ? ? ?
? ? ? ? ? ? if (view == null) {
? ? ? ? ? ? ? ? if (-1 == name.indexOf('.')) {
? ? ? ? ? ? ? ? ? ? view = onCreateView(parent, name, attrs);
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? view = createView(name, null, attrs);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }


? ? ? ? ? ? if (DEBUG) System.out.println("Created view is: " + view);
? ? ? ? ? ? return view;


? ? ? ? } catch (InflateException e) {
? ? ? ? ? ? throw e;


? ? ? ? } catch (ClassNotFoundException e) {
? ? ? ? ? ? InflateException ie = new InflateException(attrs.getPositionDescription()
? ? ? ? ? ? ? ? ? ? + ": Error inflating class " + name);
? ? ? ? ? ? ie.initCause(e);
? ? ? ? ? ? throw ie;


? ? ? ? } catch (Exception e) {
? ? ? ? ? ? InflateException ie = new InflateException(attrs.getPositionDescription()
? ? ? ? ? ? ? ? ? ? + ": Error inflating class " + name);
? ? ? ? ? ? ie.initCause(e);
? ? ? ? ? ? throw ie;
? ? ? ? }
? ? }


里面根据不同情况,调用了onCreateView方法,利用反射来创建view。其中可以使用指定的factory来创建view,这样的钩子设计使得inflate方法变得十分灵活。


然后调用rInflate(parser, temp, attrs, true)方法来递归查找temp中的子view,并添加到上层view中:


void rInflate(XmlPullParser parser, View parent, final AttributeSet attrs,
? ? ? ? ? ? boolean finishInflate) throws XmlPullParserException, IOException {


? ? ? ? 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();
? ? ? ? ? ?
? ? ? ? ? ? if (TAG_REQUEST_FOCUS.equals(name)) {
? ? ? ? ? ? ? ? parseRequestFocus(parser, parent);
? ? ? ? ? ? } else if (TAG_INCLUDE.equals(name)) {
? ? ? ? ? ? ? ? if (parser.getDepth() == 0) {
? ? ? ? ? ? ? ? ? ? throw new InflateException(" cannot be the root element");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? parseInclude(parser, parent, attrs);
? ? ? ? ? ? } else if (TAG_MERGE.equals(name)) {
? ? ? ? ? ? ? ? throw new InflateExcep

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android线程间异步通信机制源码分.. 下一篇从Android源码分析View绘制

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: