android - LayoutInflater是不是每次都加载xml?

标签 android android-layout android-inflate performance

LayoutInflater 是否每次加载 View 时都加载 xml 布局资源?

View view = LayoutInflater.from(context).inflate(R.layout.resource, null);

例如,如果我想创建 100 个具有相同布局 ID 的 View ,它会解析 100 次相同的 XML 文件还是有一些 android 框架的缓存系统?

最佳答案

当您查看 inflate 代码流时,您可以看到显然使用了多个缓存:

我首先看到:

View view = tryInflatePrecompiled(resource, res, root, attachToRoot);

然后在ResourceImpl XmlResourceParser loadXmlResourceParser中另一个缓存

 @NonNull
    XmlResourceParser loadXmlResourceParser(@NonNull String file, @AnyRes int id, int assetCookie,
            @NonNull String type)
            throws NotFoundException {
        if (id != 0) {
            try {
                synchronized (mCachedXmlBlocks) {
                    final int[] cachedXmlBlockCookies = mCachedXmlBlockCookies;
                    final String[] cachedXmlBlockFiles = mCachedXmlBlockFiles;

关于android - LayoutInflater是不是每次都加载xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12525345/

相关文章:

android - 当我在上面打字时,EditText 会变大

android - 膨胀成 "this"?

java - 根据位置在ListView中膨胀自定义View

android - ionic : Why the Android keyboard "next" button try to validate a form?

Android,如何清除大多数手机中可以从主页按钮获得的最近任务列表?反射(reflection)是一种可能的方式吗?

Android 多行数字 EditText

java - Android ScrollView 不以相对布局滚动

Android:如果任务管理器终止,则重新调用应用程序

Android Border Less 按钮按下状态背景色

android - 从布局 android-support-v4 中膨胀 fragment 时出错