免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1166 | 回复: 0
打印 上一主题 下一主题

Android110509: LayoutInflater注记 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-21 08:41 |只看该作者 |倒序浏览
Android110509: LayoutInflater注记


Email:    zcatt@163.com
Blog    http://zcatt.blog.chinaunix.net
 
内容提要
LayoutInflater及<merge>, <include>和<requestFocus>用法简记.以供备忘和参考。

声明
仅限学习交流,禁止商业用途。转载需注明出处。

版本记录
Date        Ver        Note
2011-05-09    0.1        Draft.  zcatt, Beijing
 
LayoutInflater根据layout xml文件, 递归调用xml tag相应的viewClass, 例如<FrameLayout>对应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提供了对<merge>, <include>, 和<requestFocus>计3个xml tag的支持. 其中的<include>和<merge>可以用来更好的组织layout xml脚本.

<include>类似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>


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

  1. <merge xmlns:android="http://schemas.android.com/apk/res/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]分别详述了<include>和<merge>的使用.
    


参考
[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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP