android LayoutFlater的使用
程序员文章站
2024-03-24 16:01:46
...
[b]一、 使用LayoutFlater目的[/b]
将layout转成view, 用于别的组件显示
[b]
二、 LayoutFlater的获取[/b]
1. 通过LayoutFlater静态方法
2. 通过Context:
两者是一样的。
[b]三、 LayoutFlater的使用:[/b]
常用的LayoutFlater方法:
参数:
[color=red]resource[/color] ID for an XML layout resource to load (e.g., R.layout.main_page)
[color=red]root[/color] 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.)
[color=red]attachToRoot[/color] 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.
其实是通过:
上面的那个是通过这个:
再调用带XmlPullParser参数的方法。
将layout转成view, 用于别的组件显示
[b]
二、 LayoutFlater的获取[/b]
1. 通过LayoutFlater静态方法
layoutInflater = LayoutInflater.from(context);
2. 通过Context:
LayoutInflater LayoutInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
两者是一样的。
[b]三、 LayoutFlater的使用:[/b]
常用的LayoutFlater方法:
public View inflate(int resource, ViewGroup root, boolean attachToRoot)
参数:
[color=red]resource[/color] ID for an XML layout resource to load (e.g., R.layout.main_page)
[color=red]root[/color] 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.)
[color=red]attachToRoot[/color] 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.
其实是通过:
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)
上面的那个是通过这个:
XmlResourceParser parser = getContext().getResources().getLayout(resource);
再调用带XmlPullParser参数的方法。