Android 动态布局和来自 Web 的代码

标签 android android-layout dynamic

我正在开发一个项目,该项目将有很多布局和代码更改。 我的问题很简单,但我对此一无所知。是否可以在后台线程上动态加载源代码 (Java) 和 XML 布局文件等内容,然后使用下载的内容(或者 Java 文件 + XML 布局)启动另一个 Activity ? 这样您就可以在线更改代码和布局并在每次启动应用程序时下载它?

提前非常感谢。

最佳答案

您可以加载类 dynamically 。但对于 xml 布局来说,这几乎是不可能的,因此您将编写自己的解析器和扩展器。不幸的是 LayoutInflater 无法膨胀外部文件。这是来自 LayoutInflater 文档:

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; it only works with an XmlPullParser returned from a compiled resource (R.something file.)

因此,您基本上只能通过代码创建所有 UI。

关于Android 动态布局和来自 Web 的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11499775/

相关文章:

c# - 通过动态引用访问嵌套类的成员时发生 StackOverflowException

android - 使用支持库崩溃的 Proguard Android 应用程序

Java-在 Android Studio 中将数据从 Activity 发送到选项卡 fragment

java - 从 XML 字符串获取值

java - android:layout_weight 考虑 FrameLayout 中嵌入 ImageViewer 的大小

C矩阵函数分割错误

java - 如何清除edittext中的文字

android-studio - 在Android Studio "Layout Captures"工具有什么用

android - 自定义 RatingBar 未显示超过 1 星

c++ - 这段代码中发生了什么?