我们在ListView的Adapter的getView方法里面经常会调用两个参数的inflate方法,
mInflater.inflate(R.layout.adv_viewpager, null)
我们可能会发现layout外层的layout_width layout_height属性都没起作用,全都变成wrap_content的值了。
这个问题就是因为我们使用了错误的参数照成的, 系统在inflate layout的时候 如果传入的root为空的话 就会忽略LayoutParams.
所以在getView里面应该调用
mInflater.inflate(R.layout.adv_viewpager, root, false)
这样在R.layout.adv_viewpager layout xml文件里面所有关于Layout的属性就都能生效。
那么attachToRoot是什么作用呢? 他会自动把layout加到View hierarchy中, 不需要手动调用root.addView,当然在Adapter.getView里面不用手动调用root.addView是因为Adapter已经帮我们做了, 所以如果我们在Adapter.getView里面传入attachToRoot为true的话,就会报错, 因为一个view不能attach两次。
Reference:
http://www.doubleencore.com/2013/05/layout-inflation-as-intended/
http://lmbj.net/blog/layoutinflater-and-layoutparams/
分享到:
相关推荐
android inflate demo,方便新手使用,下载导入android studio就可直接运行,简单明了的展现inflate不同参数的区别
主要介绍了Android LayoutInflater分析的相关资料,需要的朋友可以参考下
本文主要介绍Android 中Inflate 方法的用法, 在开发Android应用过程中,可以在程序中应用 Inflate()方法加载新布局,希望能帮助有需要的朋友
帮助你更好的理解LayoutInflate中的inflate方法,以后想怎么用怎么用
LayoutInflater源码分析 inflate方法的区别
Android LayoutInflater.inflate...我尝试在Google官方文档与网络上其他讨论中寻找有关的说明,而后发现许多人不但不清楚LayoutInflater的inflate()方法的细节,而且甚至在误用它。 这里的困惑很大程度上是因为Google
主要介绍了Android LayoutInflater.inflate源码分析的相关资料,需要的朋友可以参考下
Android 中LayoutInflater.inflate()方法的介绍 最近一直想弄明白LayoutInflater对象的inflate方法的用法,今天做了实例。 <LinearLayout android:id=@+id/ll_item_Group android:layout_width=match_parent ...
博客《ListView滑动删除实现之一——merge标签与LayoutInflater.inflate()》对应源码,博客地址:http://blog.csdn.net/harvic880925/article/details/45155965
Android 中LayoutInflater的使用
NULL 博文链接:https://yufeng-zhu.iteye.com/blog/1676207
通过LayoutInflater.Factory实现主题切换功能,详见:http://blog.csdn.net/llew2011
LayoutInflater Layout 与 Inflater[扩充]两个单词的合并,可以理解为:当前Activity 布局的扩充.
LayoutInflater.from(context).inflate()方法的调研
3.我们定义View的时候,如果需要在布局中使用,则必须实现带AttributeSet参数的构造方法,这又是为什么呢? 既然在这篇文章提出来,那说明这三个问题都是跟LayoutInflater脱不了干系的。在我们的分析过程中,会对...
android中LayoutInflater的使用.pdf