一、 使用LayoutFlater目的
将layout转成view, 用于别的组件显示
二、 LayoutFlater的获取
1. 通过LayoutFlater静态方法
Java代码
layoutInflater = LayoutInflater.from(context);
layoutInflater = LayoutInflater.from(context);
2. 通过Context:
Java代码
LayoutInflater LayoutInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater LayoutInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
两者是一样的。
三、 LayoutFlater的使用:
常用的LayoutFlater方法:
Java代码
public View inflate(int resource, ViewGroup root, boolean attachToRoot)
public View inflate(int resource, ViewGroup root, boolean attachToRoot)
参数:
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 (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)
attachToRoot Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.
其实是通过:
Java代码
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)
上面的那个是通过这个:
Java代码
XmlResourceParser parser = getContext().getResources().getLayout(resource);
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。