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, 例如
- 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);
- ... ... ...
-
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提供了对
- <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>
- <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