Android View的setContentView和inflate

来源:互联网 发布:思科网络技术学校pdf 编辑:程序博客网 时间:2024/05/22 12:40

android的layout文件会被解析成一个view树,这个过程通过inflate来实现。

在setContentView()方法中,Android会自动在布局文件的最外层再嵌套一个FrameLayout,所以layout_width和layout_height属性才会有效果。

inflate方法过程

首先通过createViewFromTag创建跟布局View

然后递归调用rInflate()方法来查找这个View下的子元素,每次递归完成后则将这个View添加到父布局当中。

这样的话,把整个布局文件都解析完成后就形成了一个完整的DOM结构,最终会把最顶层的根布局返回,至此inflate()过程全部结束。

layout_width和layout_height的属性无效问题

http://blog.csdn.net/guolin_blog/article/details/12921889中提到一个问题

将Button的layout_width和layout_height的值修改成多少,都不会有任何效果的

注意这句mainLayout.addView(buttonLayout);  

如果改为

LayoutParams para=new LinearLayout.LayoutParams(500,500);
        mainLayout.addView(buttonLayout, para);

就有效了viewGroup在addview时,view和viewGroup之间的关系必须要有para信息。

郭神文中,改为用RealativeLayout包住Button,那这个RelativeLayout的宽高信息(xml里)其实也是没用的。

但是setContentView时,宽高信息是有效的,估计内部肯定调的是2参的addview


参考资料

http://blog.csdn.net/guolin_blog/article/details/17357967





0 0
原创粉丝点击