码迷,mamicode.com
首页 > 其他好文 > 详细

我答同行问(续二)

时间:2015-03-11 15:00:21      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

    本片文章出自http://blog.csdn.net/andywuchuanlong,转载请说明出处,谢谢!

    我答同行问序列目录http://blog.csdn.net/andywuchuanlong/article/details/44194043

    3、使用View.inflate(context, resource, root)加载布局文件的时候,如果root为null,为什么布局文件的根节点设置的属性例如外边距、高度等都不起作用?

    分析这个问题的时候需要从远源码进行分析了,假设参数root为nullinflater.inflate(R.layout.item_list, parent,false);调用方式如下:

<span style="color: rgb(51, 51, 51);">public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {</span><span style="color:#ff0000;">// attachToRoot  false</span><span style="color:#333333;">
        synchronized (mConstructorArgs) {
            final AttributeSet attrs = Xml.asAttributeSet(parser);
            Context lastContext = (Context)mConstructorArgs[0];
            mConstructorArgs[0] = mContext;
          </span><span style="color:#ff0000;">  View result = root;// 其实是resutl = null</span><span style="color:#333333;">
            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();
                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, attrs, false);
                } else {
                    // Temp is the root view that was found in the xml
                    View temp;
                    if (TAG_1995.equals(name)) {
                        temp = new BlinkLayout(mContext, attrs);
                    } else {
                        temp = createViewFromTag(root, name, 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) {   </span><span style="color:#ff0000;">// true</span><span style="color:#333333;">
                            // 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
                    rInflate(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) {
                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;
        }
    }</span>

代码比较长,我们重点关注下面的代码

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);
      }
        }
这些代码的意思就是,当我们传进来的root参数不是空的时候,并且attachToRoot是false的时候,也就是上面的TwoActivity的实现方式的时候,会给temp设置一个LayoutParams参数。那么这个temp又是干嘛的呢?
<pre name="code" class="java">// 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;
  }
现在应该明白了吧,当我们传进来的root不是null,并且第三个参数是false的时候,这个temp就被加入到了root中,并且把root当作最终的返回值返回了。而当我们设置root为空的时候,没有设置 LayoutParams参数的temp对象,作为返回值返回了。

因此,我们可以得出下面的结论:

1.若我们采用 convertView = inflater.inflate(R.layout.item_list, null);方式填充视图,item布局中的根视图的layout_XX属性会被忽略掉,然后设置成默认的包裹内容方式

2.如果我们想保证item的视图中的参数不被改变,我们需要使用convertView = inflater.inflate(R.layout.item_list, parent,false);这种方式进行视图的填充

3.除了使用这种方式,我们还可以设置item布局的根视图为包裹内容,然后设置内部控件的高度等属性,这样就不会修改显示方式了。

简单总结一下:

  当root为null的时候,我们只是把一个xml文件实例化成view对象,返回的就是这个xml对应的view。当root不为空的时候,也就是parent存在,则将实例化后的view对象添加进parent中,然后返回

我答同行问(续二)

标签:

原文地址:http://blog.csdn.net/andywuchuanlong/article/details/44196103

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!