android service layoutinflater,Android LayoutInflater.from().inflate()源码解析

我们知道,在Activity#setContentView()中会调用PhoneWindow#setContentView()。而在PhoneWindow#setContentView()中有这么一句mLayoutInflater.inflate(layoutResID, mContentParent)。这行代码的作用是将我们的activity_main.xml填充到mContentParent中去。详见:setContentView源码解析。在写adapter的时候,也经常写mInflater.inflate(layoutResID, null)。那么,这行代码怎么就将xml文件转换成了View或者ViewGroup了呢?

获取LayoutInflater对象无非以下两种方式:

LayoutInflater.from(Context context);

LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

其实这俩是同一种方式,首先看下LayoutInflater#from()

源码位置:frameworks/base/core/java/android/view/LayoutInflater.java

LayoutInflater#from()

public static LayoutInflater from(Context context) {

LayoutInflater LayoutInflater =

(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (LayoutInflater == null) {

throw new AssertionError("LayoutInflater not found.");

}

return LayoutInflater;

}

第一种获取LayoutInflater对象的方式,不过就是对第二种方式的一个简单封装。实际上还是一回事。Context的实现类是ContextImpl,跟进。

源码位置:frameworks/base/core/java/android/app/ContextImpl.java

ContextImpl#getSystemService()

@Override

public Object getSystemService(String name) {

return SystemServiceRegistry.getSystemService(this, name);

}

跟进。

源码位置:frameworks/base/core/java/android/app/SystemServiceRegistry.java

SystemServiceRegistry#getSystemService()

public static Object getSystemService(ContextImpl ctx, String name) {

ServiceFetcher> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);

return fetcher != null ? fetcher.getService(ctx) : null;

}

直接从全局变量SYSTEM_SERVICE_FETCHERS中依据名字就get到了fetcher,之后依据fetcher直接get到了LayoutInflater对象。大写的懵B~原来啊,在SystemServiceRegistry中有个静态代码块,先看下这部分。

static {

...

registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,

new CachedServiceFetcher() {

@Override

public LayoutInflater createService(ContextImpl ctx) {

return new PhoneLayoutInflater(ctx.getOuterContext());

}});

...

}

private static void registerService(String serviceName, Class serviceClass,

ServiceFetcher serviceFetcher) {

SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);

SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);

}

static abstract class CachedServiceFetcher implements ServiceFetcher {

private final int mCacheIndex;

public CachedServiceFetcher() {

mCacheIndex = sServiceCacheSize++;

}

@Override

@SuppressWarnings("unchecked")

public final T getService(ContextImpl ctx) {

final Object[] cache = ctx.mServiceCache;

synchronized (cache) {

// Fetch or create the service.

Object service = cache[mCacheIndex];

if (service == null) {

service = createService(ctx);

cache[mCacheIndex] = service;

}

return (T)service;

}

}

public abstract T createService(ContextImpl ctx);

}

这里连续贴了两个方法和一个抽象内部类CachedServiceFetcher。由于在抽象方法CachedServiceFetcher#createService()的具体实现中返回的是PhoneLayoutInflater,所以后文中使用的一直是PhoneLayoutInflater的对象。获取LayoutInflater对象(其实是其子类PhoneLayoutInflater对象)之后,调用LayoutInflater#inflate()。跟进。

源码位置:frameworks/base/core/java/android/view/LayoutInflater.java

LayoutInflater#inflate()

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {

return inflate(resource, root, root != null);

}

这里以setContentView中的mLayoutInflater.inflate(layoutResID, mContentParent)为例,顺带也会讲解adapter中mInflater.inflate(layoutResID,null)这种情况。也就是root参数为null和不为null两种情况。root==null,则第三个参数为false.root!=null,则第三个参数为true。跟进。

LayoutInflater#inflate()

public 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();

}

}

跟进。

LayoutInflater#inflate()

public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {

synchronized (mConstructorArgs) {

...

View result = root;

try {

...

// 获取根节点的字符串,例如LinearLayout

final String name = parser.getName();

// 根节点merge开头

if (TAG_MERGE.equals(name)) {

...

} else {

// 创建根视图View

final View temp = createViewFromTag(root, name, inflaterContext, attrs);

ViewGroup.LayoutParams params = null;

if (root != null) {

// 获取LayoutParams

params = root.generateLayoutParams(attrs);

if (!attachToRoot) {

// 应用LayoutParams到根节点View

temp.setLayoutParams(params);

}

}

// 遍历解析子View,并添加到根节点temp中

rInflateChildren(parser, temp, attrs, true);

// root不为空,直接将根节点View添加到root中

if (root != null && attachToRoot) {

root.addView(temp, params);

}

// root等于null,直接返回根节点temp

if (root == null || !attachToRoot) {

result = temp;

}

}

}catch (Exception e) {

...

}

return result;

}

}

上面每一步都有注释,下面重点看下生成根节点View的createViewFromTag()和遍历生成子View的rInflateChildren()方法。

LayoutInflater#createViewFromTag()

private View createViewFromTag(View parent, String name, Context context, AttributeSet attrs) {

return createViewFromTag(parent, name, context, attrs, false);

}

View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,

boolean ignoreThemeAttr) {

...

if (-1 == name.indexOf('.')) {

view = onCreateView(parent, name, attrs);

} else {

view = createView(name, null, attrs);

}

...

return view;

}

跟进。

LayoutInflater#createView()

public final View createView(String name, String prefix, AttributeSet attrs)

throws ClassNotFoundException, InflateException {

Constructor extends View> constructor = sConstructorMap.get(name);

Class extends View> clazz = null;

try {

if (constructor == null) {

clazz = mContext.getClassLoader().loadClass(

prefix != null ? (prefix + name) : name).asSubclass(View.class);

...

constructor = clazz.getConstructor(mConstructorSignature);

constructor.setAccessible(true);

sConstructorMap.put(name, constructor);

} else {

...

}

final View view = constructor.newInstance(args);

return view;

} catch (Exception e) {

...

}

}

sConstructorMap是个HashMap>对象。首先依据根节点的名字,例如LinearLayout去查找缓存的构造器,如果是第一次执行,肯定返回null。如果返回为null,则通过反射出构造方法,并强制设置可访问,之后存进sConstructorMap中。如果缓存中有构造器,那么直接取出。最后调用newInstance反射出根节点View实例。得到根节点View实例之后,接着设置属性,最后调用rInflateChildren()遍历创建子View。跟进。

LayoutInflater#rInflateChildren()

final void rInflateChildren(XmlPullParser parser, View parent, AttributeSet attrs,

boolean finishInflate) throws XmlPullParserException, IOException {

rInflate(parser, parent, parent.getContext(), attrs, finishInflate);

}

parent参数是根节点View。这里只是简单转发给rInflate()方法处理。跟进。

LayoutInflater#rInflateChildren()

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)) {

...

}

...

} 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();

}

}

遍历体现在While循环上,name为子节点View的名称,例如:TextView,RelativeLayout等。几个以tag、include等开头的子节点走最上面几个if的逻辑,我们的重点在于寻常View走的else逻辑。可以看到:首先,和创建根节点View调用同一个方法createViewFromTag()创建子View,紧接着设置子View的参数,然后调用递归调用rInflateChildren()方法再去测量子节点的所有View,最后才将子节点添加到父布局,这个父布局可能是根节点,也可能是某个子节点。遍历结束之后,所有子View也添加到布局当中并设置好相应的布局参数。

至此,LayoutInflater.from().inflate()源码解析结束~

更多Framework源码解析,请移步 Framework源码解析系列[目录]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这个方法是在Android开发中使用的,可以通过LayoutInflater类的from方法获取到一个LayoutInflater实例,然后调用其inflate方法来加载布局文件,将其转化为一个View对象,以供使用。 ### 回答2: layoutinflater.from(context).inflateAndroid中一个比较常见的方法,主要用于将布局文件转换成可在代码中使用的View对象。以下是详细的解释: 首先,LayoutInflater是一个Android系统类,其作用是将布局文件转换成可在代码中实现的View对象。而从context.getParameter()方法中返回一个LayoutInflater示例后,需要使用其inflate()方法来加载布局文件,返回一个View对象。inflate()方法有三个参数:布局文件ID、父View以及一个布尔标志。 在大多数情况下,第二个参数都为null,这意味着在加载布局文件时没有父元素。而第三个参数标志通常设置为false,这意味着在加载布局文件时不附加给指定父元素。 因此,调用LayoutInflater.from(context).inflate(R.layout.my_layout, null, false)会返回一个View对象,该对象表示my_layout.xml布局文件的内容。可以将此对象添加到任何视图层次结构中,例如: ViewGroup parent = findViewById(R.id.parent_layout); View child = LayoutInflater.from(context).inflate(R.layout.my_layout, parent, true); parent.addView(child); 在这种情况下,inflate()方法的第二个参数是父元素的引用,表示新加载的View对象将成为此父元素的一部分。第三个参数标志设置为true,这意味着从布局文件加载的视图将自动成为传递给inflate()方法的父元素的一部分。 总之,LayoutInflater.from(context).inflateAndroid开发中非常有用的一个方法,它使您可以轻松地将布局文件转换为可在代码中操作的View对象。了解它的用法可以使您更轻松地开发高质量的Android应用程序。 ### 回答3: layoutinflater.from(context).inflateAndroid 中一种常见的布局填充方法。在 Android 中,我们通常使用 XML 文件创建布局,然后使用 Java 代码调用该布局以填充视图。其中,layoutinflater.from(context) 是获取一个 LayoutInflater 对象的方法,它可以用于动态将布局文件转换为其对应的视图对象并在当前视图中添加。 在上述代码中,context 是用于创建视图的上下文对象,inflate 是用于执行布局填充的方法。该方法中需要传入一个布局文件ID,该 ID 用于确定要填充的布局文件的位置和名称。被填充的布局文件中包含了布局中的所有 View 对象及其属性,包括控件大小、边距、背景等等。填充完成后,该布局文件中的所有视图都将被转换为 Java 中的 View 类型,并作为整个填充视图的一部分添加到 ViewGroup 中。 通常情况下,我们会在 Activity 中调用该方法以填充视图对象,然后通过 findViewById 对填充好的视图对象中的控件进行定位,并对其进行任何必要的更改或操作。这样,我们就可以通过代码实现 UI 界面中各种复杂的布局效果,并实现超出 XML 文件所能实现的更高级的 UI 界面效果。 总之,layoutinflater.from(context).inflateAndroid 中一种非常常见的布局填充方法,它允许我们将布局文件转换为对应的 View 对象,并添加到当前视图中。这种方法在 Android 应用程序开发中非常有用,特别是在创建 UI 界面时。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值