我答同行问(续二)

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

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

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

分析这个问题的时候需要从远源码进行分析了,假设参数root为null,inflater.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中,然后返回

时间: 2024-08-29 08:31:48

我答同行问(续二)的相关文章

我答同行问(续四)

本片文章出自http://blog.csdn.net/andywuchuanlong,转载请说明出处,谢谢!  我答同行问序列目录http://blog.csdn.net/andywuchuanlong/article/details/44194043 5.为什么ScrollView中嵌套了ViewPager后,viewPager高度出现问题并且滑动失效? ScrollView是可以滑动的,而viewPager也是可以进行滑动的,虽然说两者嵌套不违反view的嵌套原则,但是Android系统里面

我答同行问(续五)

本片文章出自http://blog.csdn.net/andywuchuanlong,转载请说明出处,谢谢!  我答同行问序列目录http://blog.csdn.net/andywuchuanlong/article/details/44194043 6.在项目中美工一般给我们切几套图,才能够适配Android碎片化的终端? 这个问题是属于Android终端屏幕适配的问题.读者可以访问我下列的文章<Android屏幕适配>一文,便大概可以回答这个问题了. 我再此处只想讲述下我在项目中的适配是

我答同行问(续一)

本片文章出自http://blog.csdn.net/andywuchuanlong,转载请说明出处,谢谢! 我答同行问序列目录http://blog.csdn.net/andywuchuanlong/article/details/44194043 1.四大组件全部结束销毁,为什么应用依然在后台运行?为什么不能真正的退出应用? 2.Android中的service是在后台运行的服务,貌似线程也是在后台异步执行,为什么service不能被线程替代? 想要回答第一个问题就需要扯到Android内存

我答同行问(续三)

本片文章出自http://blog.csdn.net/andywuchuanlong,转载请说明出处,谢谢!  我答同行问序列目录http://blog.csdn.net/andywuchuanlong/article/details/44194043 4.网络请求的时候,我们都需要开启线程,那么是使用asyncTask还是使用Thread+Handler模式呢? 网络请求是每个app都需要进行的,很多人会使用asyncTask,也有人喜欢Thread+Handler,下面我按照我的想法讲解一下

我答同行问

    本片文章出自http://blog.csdn.net/andywuchuanlong,转载请说明出处,谢谢! 之前遇到同行问的一些问题,能记住的我都将其写出来,供初学者参考.问题如下: 1.四大组件全部结束销毁,为什么应用依然在后台运行?为什么不能真正的退出应用? 2.Android中的service是在后台运行的服务,貌似线程也是在后台异步执行,为什么service不能被线程替代? 3.使用View.inflate(context, resource, root)加载布局文件的时候,如

答读者问(2)

五一节回来,打开邮箱,我又看到一些读者发过来的邮件.在此,我挑选了一封一位在校研究生的邮件进行回复.回复的内容仅是我个人结合自身经历所表达出的观点,不当之处,还请批评指正. 邮件部分内容如下: 师哥你好, 我是CSDN的XXX,XXX大学的研二学生,我现在感觉自己很浮躁,感觉每天恍恍惚惚的,学什么都不能集中注意力,知道自己有很多的东西需要学,有很多的事需要干,但就是踏实不下来. 主要是存在的问题如下: 1.小论文还没发表,每天被这个事情牵涉一些精力,看书的时候有的时候想起小论文还没发表,心思就没

答读者问(7):有关实习、毕业论文及软件开发和测试的关系等问题

最近收到一位研究生朋友的邮件,让我想到自己研究生毕业之前,也曾有过很多的疑惑,希望得到过来人的解答.互联网不仅是我们最好的老师,同时也是最好的桥梁.我们都要感谢并善于利用它. 闲话不说,言归正传.邮件原文如下: 周前辈,您好 我是XXX研究生,我叫XXX.专业是信息与通信工程.现在研二,过了暑假马上就研三了.我在CSDN上无意间看到您的一些文章,写的很好,感触很多.所以就一直在关注您! 下面我简单说下我的情况,我本科和研究生到目前,还没有工作过,也没有实习过.这个暑假,我找了一个实习,是XXX公

答读者问(8):有关Java学习的相关问题

最近,我收到一位研究生朋友的邮件,大致内容如下: 周老师您好,我是XXX大学软件工程专业的一名研究生我叫XXX,学习的方向是java,有些问题不知道周老师能否帮我解惑下,在此谢谢老师! 1.我应该专注于后台的学习么?我在实际学习中,感觉到前台和后台也是分不开的总要涉及到,学习的过程中我学习了SSH大量的知识,再回头看看前台的一些技术jsp,html,css,jQuery感觉技术太多了,需要大量的练习记忆,不知道到底应该如何学习这些知识? 2.精通SSH(在此以SSH举个例子)等等技术到底需要个什

央行发文深入推进农村支付服务环境建设并答记者问

昨日,央行发布<全面推进深化农村支付服务环境建设的指导意见>,主要从深化助农取款服务,优化农民工银行卡特色服务,推广非现金支付等方面,对下一步深化农村支付环境建设工作提出要求. 一是将深化助农取款服务作为核心内容.允许在银行卡助农取款服务点新增开办现金汇款.转账汇款.代理缴费三种业务,丰富服务功能. 二是明确服务点业务收费要兼顾可持续发展和适度优惠农民的指导原则.特别是对于农村老人在服务点支取养老金等政府涉农补贴资金,要求每卡每月首笔取款业务免费. 三是鼓励支持农村支付服务主体多元化发展,推动