Dynamically inflates UI in Android App

2023-11-18

Dynamically inflates UI in Android App

There is a fascinating idea that inflates UI according to an android layout xml downloaded from server. According to the Android API, it looks quite feasible.

One of LayoutInflate.inflate method overloads accept Layout Xml as XmlPullParser.

And XmlPullParser can wrap around an input stream, so as consequence, following code seems to be working:

Inflate view on the fly
      
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
      
      
      
      
public class DynamicView extends FrameLayout {
public DynamicView(Context context, InputStream layoutData) throws XmlPullParserException {
super(context);
createView(context, layoutData);
}
private void createView(Context context, InputStream layoutData) throws XmlPullParserException {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware( true);
XmlPullParser parser = factory.newPullParser();
parser.setInput(layoutData, "UTF-8");
inflater.inflate(parser, this, true);
}
}

The code looks great, compiling looks fine, but when the code excuted, an exception is thrown by the inflater.

According to the LayoutInflater document, this approach won’t work(at least for now, it won’t).

For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime.

LayoutInflater Document developer.android.com

Actually, Android compiler compiles the layout xml files into binary xml block, which has convert attributes into some special format. And in Android SDK, the LayoutInflater uses XmlResourceParser instead of plain XmlPullParser, which is created byXmlBlock.

XmlBlock is an internal class used in Resources to cache the binary xml document.

And there is no way to create XmlResourceParser or other classes to inject custom behavior into this process. Personally assume that it is pretty much related to the Android Resource and Theming mechanism, there are quite a number cross references between resources. To make it works in a efficient way, Android Runtime did a lot work, such as cache and pre-processing. To override this behavior require quite a number of work and need to aware of potential performance issue. Since the inflating could happen quite often during navigation.

As a not that fansy alternative, UI based on HTML hosted in WebView could be considered.

转自:http://timnew.me/blog/2013/12/13/dynamically-inflates-ui-in-android-app/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Dynamically inflates UI in Android App 的相关文章

随机推荐

  • Java设计模式-结构型设计模式-适配器模式

    Java设计模式 结构型设计模式 适配器模式 从这一专栏开始将学习设计模式 上课学习和自己总结归纳的笔记将总结出来供大家参考 参考书籍 设计模式就该这样学 其他文章 Java设计模式 UML类图 Java设计模式 七大架构设计原则 开闭原则
  • 机器学习NLP参考文章

    本站整理了一些NLP的入门资料参考 建议初学者看看 需要复制链接在浏览器里打开 1 通过kaggle比赛学习机器学习文本分类方法https zhuanlan zhihu com p 34899693 utm medium social ut
  • 宋浩高等数学笔记(八)向量代数与空间解析几何

    本章知识点并不难理解 但是公式与名词属于非常多 记忆时需重点对待
  • Java GUI编程——在线聊天室

    引言 综合应用Java的GUI编程和网络编程 实现一个能够支持多组用户同时使用的聊天室软件 该聊天室具有比较友好的GUI界面 并使用C S模式 支持多个用户同时使用 用户可以自己选择加入或者创建房间 和房间内的其他用户互发信息 文字和图片
  • Web前端的优点有什么?为什么前端可以这么火?

    今天小编要跟大家分享的文章是关于web前端的优点有哪些 为什么Web前端可以这么火 相信小伙伴们对Web前端并不陌生 那么你知道Web前端的有点都有哪些吗 下面就让我们一起来看一看吧 HTML5是唯一一个通吃PC Mac iPhone iP
  • oracle两个日期的月份间隔,Oracle 计算两个日期间隔的天数、月数和年数

    在Oracle中计算两个日期间隔的天数 月数和年数 一 天数 在Oracle中 两个日期直接相减 便可以得到天数 1 select to date 08 06 2015 mm dd yyyy to date 07 01 2015 mm dd
  • 三招搞定你的ubuntu安全问题

    本篇主要介绍以下三个部分 反病毒引擎clamav的安装和使用 ubuntu ufw限制访问地址 ubuntu用户连接失败锁定指定时间 反病毒引擎clamav的安装和使用 简介 ClamAV是一款开源的反病毒引擎 用于检测病毒 特洛伊木马 恶
  • IT行业里的热门技术和项目分享

    随着科技的发展 IT行业中涌现出了很多热门技术 其中最具代表性的包括人工智能和机器学习 云计算和云原生技术 大数据和数据分析 容器化技术和Kubernetes 前端框架和组件库等 此外 也有一些热门IT技术项目备受关注 比如Apache K
  • jdk报错

    Syntax error annotations are only available if source level is 1 5 or greater解决方法 原创 2016年07月18日 14 13 39 ul class artic
  • Java内存分配全面浅析

    本文将由浅入深详细介绍Java内存分配的原理 以帮助新手更轻松的学习Java 这类文章网上有很多 但大多比较零碎 本文从认知过程角度出发 将带给读者一个系统的介绍 进入正题前首先要知道的是Java程序运行在JVM Java VirtualM
  • 多维随机变量及其分布(四):

    一 二维随机变量及其分布函数 1 二维随机变量 设随机变量 Z X Y 则有 Z X Y 一个随机变量是有两个随机变量决定的 2 联合分布函数的基本性质 单调性 F x y 分别对x 或y是单调不减的 即 对任意固定的y 当 x1 lt x
  • 【SPI】STM32 SPI 双机通信,SPI从机模式使用

    文章目录 一 SPI主机配置 二 SPI从机配置 三 双机通信 1 轮询 中断 低速 2 轮询 DMA 低速 3 DMA DMA 高速 4 开启CRC校验 自选 四 遇到的问题 1 高速使用时 程序卡死 或者数据出错 已解决 2 数据莫名其
  • html制作日程安排,在线日程安排怎样做?日程表在线制作工具

    在线日程安排怎样做 日程表在线制作工具 在线日程安排怎样做 每日仅有二十四小时 可在一天我们要做的事却太多 每日忙的晕头转向 身心俱疲 这可咋办啊 戴尔 麦康基说过 计划的订制比计划本身更为重要 因此可见 订制好每日的行程安排是不可或缺的
  • 详述String类的equals方法

    详述String类的equals方法 1 两个String类的对象采取直接赋值 ublic class Test public static void main String args String name1 Tom String nam
  • java基础语法之面向对象

    面向对象 面向对象是一种编程思想 与之对应的是面向过程 区别 面向过程 POP 强调的是功能 面向对象 OOP 强调的是带有具体功能的对象 面向对象的优点 提高代码复用性 降低代码间的耦合度 提升代码维护性 三大特征 封装 继承 多态 一
  • 六种黑客入侵手机的常见方式

    六种黑客入侵手机的常见方式 在移动网络科技高速发展的今天 我们每个人的手机都有可能成为黑客攻击的对象 下面为大家介绍6种黑客入侵手机的常见方式 希望能够帮助大家避免手机被不对象攻击 1 网络钓鱼攻击 网络钓鱼攻击非常普遍 那是因为它们非常有
  • 磁盘空间重分配

    root localhost df h Filesystem Size Used Avail Use Mounted on dev mapper VolGroup lv root 50G 47G 16M 100 lv root满了 tmpf
  • pageHelper的使用与源码分析

    文章目录 一 使用步骤 二 分页原理 1 统计总数 2 源码分析 三 总结 pageHelper作为Mybatis最好用的分页插件 自然受到极大多数人的追捧 而这里想要尽量阐述清楚pageHelper的具体使用步骤 实现的背后原理 以及与原
  • WebGL 实践篇(五)三维图形的绘制及矩阵变换、正射投影

    一 三维 F 的绘制 1 着色器 按照上一篇提到的矩阵变换 我们可以直接在顶点着色器中加入相应的矩阵变换 这样就可以简化着色器代码 通过变量传入矩阵的值也便于之后矩阵变换的修改 三维图形的绘制相比于二维图形只在参数类型上有一些变化 注意ve
  • Dynamically inflates UI in Android App

    Dynamically inflates UI in Android App There is a fascinating idea that inflates UI according to an android layout xml d