Android110509: LayoutInflater注记
Email: zcatt@163.com
Blog http://zcatt.blog.chinaunix.net
内容提要
LayoutInflater及
, 和用法简记.以供备忘和参考。
声明
仅限学习交流,禁止商业用途。转载需注明出处。
版本记录
Date Ver Note
2011-05-09 0.1 Draft. zcatt, Beijing
LayoutInflater根据layout xml文件, 递归调用xml tag相应的viewClass, 例如对应FrameLayout, 的构造函数生成viewClass的实例. 调用的viewClass的构造函数的signature如下,
- public View(Context context, AttributeSet attrs);
通常使用LayoutInflater的步骤是两步:
1) 取得LayoutInflater实例.
- private LayoutInflater mInflater;
-
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
或
- private LayoutInflater mInflater;
-
mInflater = LayoutInflater.from(context);
在contextImpl.java我们可以找到如下代码,
- ... ... ...
-
public Object getSystemService(String name) {
-
if (WINDOW_SERVICE.equals(name)) {
-
return WindowManagerImpl.getDefault();
-
} else if (LAYOUT_INFLATER_SERVICE.equals(name)) {
-
synchronized (mSync) {
-
LayoutInflater inflater = mLayoutInflater;
-
if (inflater != null) {
-
return inflater;
-
}
-
mLayoutInflater = inflater =
-
PolicyManager.makeNewLayoutInflater(getOuterContext());
-
return inflater;
-
}
-
} else if (ACTIVITY_SERVICE.equals(name)) {
-
return getActivityManager();
-
... ... ...
而PolicyManager.makeNewLayoutInflater()实际是调用 Policy.makeNewLayoutInflater()
- public class Policy implements IPolicy {
-
... ... ...
-
-
public PhoneLayoutInflater makeNewLayoutInflater(Context context) {
-
return new PhoneLayoutInflater(context);
-
}
-
... ... ...
-
}
也就是说,Android中实际使用的是LayoutInflater的子类 PhoneLayoutInflater.
LayoutInflater
PhoneLayoutInflater
2) 调用LayoutInflater.infate()载入指定的layout xml, 生成对应的View. inflate()入口参数可以控制是否将生成的view加到root中. 这里罗列LayoutInflater的几个inflate().
- public View inflate(int resource, ViewGroup root);
-
public View inflate(XmlPullParser parser, ViewGroup root);
-
public View inflate(int resource, ViewGroup root, boolean attachToRoot);
-
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot);
在LayoutInflater可以设置Filter, 不满足Filter条件的view将不会生成, 并抛例外exception, 较少用到, 不赘述, 可参考RemoteView.
LayoutInflater提供了对, , 和计3个xml tag的支持. 其中的和可以用来更好的组织layout xml脚本.
类似C的#include语句. 粘贴[1]中的例子
- <com.android.launcher.Workspace
-
android:id="@+id/workspace"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
-
launcher:defaultScreen="1">
-
-
<include android:id="@+id/cell1" layout="@layout/workspace_screen" />
-
<include android:id="@+id/cell2" layout="@layout/workspace_screen" />
-
<include android:id="@+id/cell3" layout="@layout/workspace_screen" />
-
-
</com.android.launcher.Workspace>
比较特殊, 只能用在xml的root tag上, 是用来消除xml的root node冗余的. 如果xml的root node同父节点是一样的, 就可以直接使用父节点做为容器, 没必要再创建一个root node. 粘贴[2]中的例子.
- <merge xmlns:android="">
-
-
<ImageView
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
-
android:scaleType="center"
-
android:src="@drawable/golden_gate" />
-
-
<TextView
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_marginBottom="20dip"
-
android:layout_gravity="center_horizontal|bottom"
-
-
android:padding="12dip"
-
-
android:background="#AA000000"
-
android:textColor="#ffffffff"
-
-
android:text="Golden Gate" />
-
-
</merge>
参考文档[1]和[2]分别详述了和的使用.
参考
[1] ANDROID_SDK/docs/resources/articles/layout-tricks-merge.html
[2] ANDROID_SDK/docs/resources/articles/layout-tricks-reuse.html
[3] froyo src
阅读(774) | 评论(0) | 转发(0) |