自定义控件中除了绘制流程涉及到的测量布局绘制的api, 还有一些api使用的也比较频繁,这里叔做了一下总结,也请同学们补充或纠正,让叔也涨涨姿势。
inflate
inflate方法常常用来解析一个xml布局文件,在自定义组合式控件中常常使用,使用的姿势包括:
View.inflate(context, resource, root)
LayoutInflater.from(getContext()).inflate(resource, root);
而View.inflate其实还是调用的LayoutInflater去解析一个xml:
public static View inflate(Context context, int resource, ViewGroup root) {
LayoutInflater factory = LayoutInflater.from(context);
return factory.inflate(resource, root);
}
所以这两种姿势没啥区别,这里来讨论一下inflate(resouce, root)的返回值,参数resource是布局资源,root是传入的一个根节点。如果root传入一个null,inflate就会解析resource对应的xml,返回这个xml中的根节点,如果root传入不为null,inflate会解析这个xml布局并且添加到根节点root下,然后返回根节点root。
另外还有带三个参数的inflate方法:
inflate(int resource, ViewGroup root, boolean attachToRoot)
这里多了一个参数attachToRoot,如果root为null&