您的位置:首页 > 其它

LayoutInflater 简要解析

2015-07-27 16:31 447 查看
LayoutInflater 是一个抽象类,在文档中如下声明:

public abstract class LayoutInflater extends Object

获得 LayoutInflater 实例的三种方式

1. LayoutInflater inflater = getLayoutInflater();//调用Activity的getLayoutInflater()
2. LayoutInflater inflater = LayoutInflater.from(context);
3. LayoutInflater inflater =  (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);


这三种方式,实质上是一样的,getLayoutInflater()会调用LayoutInflater.from(context),LayoutInflater.from(context)会调用第三个。

public View inflate (int resource, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)
public View inflate (int resource, ViewGroup root, boolean attachToRoot)


root参数,通常为null,

Parameters:
resource ID for an XML layout resource to load (e.g., R.layout.main_page)
root Optional view to be the parent of the generated hierarchy.
Returns:
The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file.


如果设置了root,将返回整个ViewGroup,否则返回填充的Layout。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  layout Inflater