博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LayoutInflater服务
阅读量:6685 次
发布时间:2019-06-25

本文共 6314 字,大约阅读时间需要 21 分钟。

hot3.png

layoutInflater服务注册在SystemServiceRegistry中。final class SystemServiceRegistry {    static{        // ...        registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,                new CachedServiceFetcher
() { @Override public LayoutInflater createService(ContextImpl ctx) { return new PhoneLayoutInflater(ctx.getOuterContext()); }}); // ... }}// PhoneLayoutInflater继承自LayoutInflaterpublic class PhoneLayoutInflater extends LayoutInflater { private static final String[] sClassPrefixList = { "android.widget.", "android.webkit.", "android.app." }; public PhoneLayoutInflater(Context context) { super(context); } protected PhoneLayoutInflater(LayoutInflater original, Context newContext) { super(original, newContext); } @Override protected View onCreateView(String name, AttributeSet attrs) throws ClassNotFoundException { for (String prefix : sClassPrefixList) { try { View view = createView(name, prefix, attrs); if (view != null) { return view; } } catch (ClassNotFoundException e) { } } return super.onCreateView(name, attrs); } public LayoutInflater cloneInContext(Context newContext) { return new PhoneLayoutInflater(this, newContext); }} 从Activity中setContentView(layoutResID)说起。// PhoneWindow < extend abstract class Window >public void setContentView(int layoutResID) { // ... if (mContentParent == null) { installDecor(); } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) { mContentParent.removeAllViews(); } if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {            final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,getContext());       transitionTo(newScene); } else {     mLayoutInflater.inflate(layoutResID, mContentParent); } // ...}private void installDecor() { if (mDecor == null) {     // ...     mDecor = generateDecor(-1);     // 备注 new DecorView(context, featureId, this, getAttributes()); < DecorView extends FrameLayout > // ... } else {     mDecor.setWindow(this); } if (mContentParent == null) {     mContentParent = generateLayout(mDecor);    // ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT); } }// LayoutInflaterpublic View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {       final Resources res = getContext().getResources();       final XmlResourceParser parser = res.getLayout(resource); try {     return inflate(parser, root, attachToRoot); } finally {     parser.close(); }}
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {    synchronized (mConstructorArgs) {        Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");        final Context inflaterContext = mContext;        final AttributeSet attrs = Xml.asAttributeSet(parser);        Context lastContext = (Context) mConstructorArgs[0];        mConstructorArgs[0] = inflaterContext;        View result = root;        try {            // Look for the root node. 寻根之旅            int type;            while ((type = parser.next()) != XmlPullParser.START_TAG &&                    type != XmlPullParser.END_DOCUMENT) {                // Empty            }            final String name = parser.getName();            if (TAG_MERGE.equals(name)) {                if (root == null || !attachToRoot) {                    // InflateException                }                rInflate(parser, root, inflaterContext, attrs, false);            } else {                // 解析并创建View  final View temp = createViewFromTag(root, name, inflaterContext, attrs);                ViewGroup.LayoutParams params = null;                if (root != null) {
params = root.generateLayoutParams(attrs); if (!attachToRoot) {
temp.setLayoutParams(params); } }                // 解析tmp下所有子View并添加到tmp下 rInflateChildren(parser, temp, attrs, true); if (root != null && attachToRoot) { root.addView(temp, params); } if (root == null || !attachToRoot) { result = temp; } } } return result; }}从上面源码可以看出:
<假定根据layoutid解析并创建的view是tmp>
1、root != null 保留tmp参数 root == null 使用默认参数赋值给tmp并返回tmp2、attachToRoot == ture && root != null; 执行 root.addView(temp, params); attachToRoot == false tmp不会添加到root下。
1、解析xml中的根标签 2、如果根标签是merge,调用rInflate会将merge下所有子view添加到根标签 3、对View进行解析
void rInflate(XmlPullParser parser, View parent, Context context,        AttributeSet attrs, boolean finishInflate) throws XmlPullParserException, IOException {    final int depth = parser.getDepth();    int type;    while (((type = parser.next()) != XmlPullParser.END_TAG ||            parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {        if (type != XmlPullParser.START_TAG) {            continue;        }        final String name = parser.getName();                if (TAG_REQUEST_FOCUS.equals(name)) {            parseRequestFocus(parser, parent);        } else if (TAG_TAG.equals(name)) {            parseViewTag(parser, parent, attrs);        } else if (TAG_INCLUDE.equals(name)) {            if (parser.getDepth() == 0) {                throw new InflateException("
cannot be the root element"); } parseInclude(parser, context, parent, attrs); } else if (TAG_MERGE.equals(name)) { throw new InflateException("
must be the root element"); } else { final View view = createViewFromTag(parent, name, context, attrs); final ViewGroup viewGroup = (ViewGroup) parent; final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs); rInflateChildren(parser, view, attrs, true); viewGroup.addView(view, params); } } if (finishInflate) { parent.onFinishInflate(); }}

转载于:https://my.oschina.net/u/3342652/blog/1542185

你可能感兴趣的文章
4.14Python数据处理篇之Matplotlib系列(十四)---动态图的绘制
查看>>
2018-2019 网络对抗技术 20165231 Exp5 MSF基础应用
查看>>
PATH环境 变量
查看>>
mysql 杂
查看>>
Jenkins执行批处理文件失败
查看>>
Docker 的插件式设计
查看>>
Loadrunner日志设置与查看
查看>>
nginx状态监控
查看>>
三层架构(★)
查看>>
我是该高兴呢还是悲伤呢?
查看>>
[LEETCODE] 72 Edit Distance
查看>>
转的es6 =>函数
查看>>
关于Unity中Shader的内置值
查看>>
Windows API一日一练(86)GetClipboardData函数
查看>>
雷林鹏分享:Yii(yiiframework)框架(二):建立第一个Yii应用
查看>>
jq第一天(1.83里面的属性)属性-》属性
查看>>
类及对象
查看>>
isinstance函数
查看>>
杭电ACM--1001
查看>>
VHDL学习:利用Quartus自带库3步快速完成状态机
查看>>