Chinaunix首页 | 论坛 | 博客
  • 博客访问: 173610
  • 博文数量: 47
  • 博客积分: 992
  • 博客等级: 准尉
  • 技术积分: 565
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-08 21:57
文章分类

全部博文(47)

文章存档

2019年(1)

2018年(1)

2017年(1)

2014年(6)

2013年(1)

2012年(2)

2011年(35)

我的朋友

分类: 嵌入式

2011-05-09 16:27:29

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如下,

  1. public View(Context context, AttributeSet attrs);


通常使用LayoutInflater的步骤是两步:

1) 取得LayoutInflater实例.

  1. private LayoutInflater mInflater;
  2. mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    或

  1. private LayoutInflater mInflater;
  2. mInflater = LayoutInflater.from(context);
在contextImpl.java我们可以找到如下代码,

  1. ... ... ...
  2.     public Object getSystemService(String name) {
  3.         if (WINDOW_SERVICE.equals(name)) {
  4.             return WindowManagerImpl.getDefault();
  5.         } else if (LAYOUT_INFLATER_SERVICE.equals(name)) {
  6.             synchronized (mSync) {
  7.                 LayoutInflater inflater = mLayoutInflater;
  8.                 if (inflater != null) {
  9.                     return inflater;
  10.                 }
  11.                 mLayoutInflater = inflater =
  12.                     PolicyManager.makeNewLayoutInflater(getOuterContext());
  13.                 return inflater;
  14.             }
  15.         } else if (ACTIVITY_SERVICE.equals(name)) {
  16.             return getActivityManager();
  17.     ... ... ...


而PolicyManager.makeNewLayoutInflater()实际是调用 Policy.makeNewLayoutInflater()

  1. public class Policy implements IPolicy {
  2.     ... ... ...

  3.     public PhoneLayoutInflater makeNewLayoutInflater(Context context) {
  4.         return new PhoneLayoutInflater(context);
  5.     }
  6.     ... ... ...
  7. }


也就是说,Android中实际使用的是LayoutInflater的子类 PhoneLayoutInflater.

    LayoutInflater
        PhoneLayoutInflater

2) 调用LayoutInflater.infate()载入指定的layout xml, 生成对应的View. inflate()入口参数可以控制是否将生成的view加到root中. 这里罗列LayoutInflater的几个inflate().

  1. public View inflate(int resource, ViewGroup root);
  2. public View inflate(XmlPullParser parser, ViewGroup root);
  3. public View inflate(int resource, ViewGroup root, boolean attachToRoot);
  4. public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot);


在LayoutInflater可以设置Filter, 不满足Filter条件的view将不会生成, 并抛例外exception, 较少用到, 不赘述, 可参考RemoteView.

LayoutInflater提供了对, , 和计3个xml tag的支持. 其中的可以用来更好的组织layout xml脚本.

类似C的#include语句. 粘贴[1]中的例子


  1. <com.android.launcher.Workspace
  2.     android:id="@+id/workspace"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"

  5.     launcher:defaultScreen="1">

  6.     <include android:id="@+id/cell1" layout="@layout/workspace_screen" />
  7.     <include android:id="@+id/cell2" layout="@layout/workspace_screen" />
  8.     <include android:id="@+id/cell3" layout="@layout/workspace_screen" />

  9. </com.android.launcher.Workspace>


比较特殊, 只能用在xml的root tag上, 是用来消除xml的root node冗余的. 如果xml的root node同父节点是一样的, 就可以直接使用父节点做为容器, 没必要再创建一个root node. 粘贴[2]中的例子.

  1. <merge xmlns:android="">

  2.     <ImageView
  3.         android:layout_width="fill_parent"
  4.         android:layout_height="fill_parent"
  5.     
  6.         android:scaleType="center"
  7.         android:src="@drawable/golden_gate" />
  8.     
  9.     <TextView
  10.         android:layout_width="wrap_content"
  11.         android:layout_height="wrap_content"
  12.         android:layout_marginBottom="20dip"
  13.         android:layout_gravity="center_horizontal|bottom"

  14.         android:padding="12dip"
  15.         
  16.         android:background="#AA000000"
  17.         android:textColor="#ffffffff"
  18.         
  19.         android:text="Golden Gate" />

  20. </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

Locations of visitors to this page
阅读(734) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~