XML解析器的动态布局(动态加载的外观)动态、布局、外观、加载

2023-09-05 07:16:38 作者:充血的你

我想编写一个应用程序,其中(至少目前如此),内容始终是相同的,但布局是动态加载在运行时根据用户preference。基本上我想要的应用程序,应用了皮肤,它可能看起来完全不同与其他的皮肤。

I want to write an app where (at least for now) the content is always the same but the layout is loaded dynamically at run time based on a user preference. Essentially I want the app to apply a "skin" which may look completely different to other skins.

我发现使用SAXParser的一些教程: http://www.androidpeople.com/android-xml-parsing-tutorial-using-saxparser/ http://twigstechtips.blogspot.com/2010/12/android-how-to-parse-xml-string.html 并且可以想象从头开始编写一些识别所有的标准XML布局标签,然后动态加载的布局的每个部分。但是,这是一个很多工作从头开始做的!当然,此功能可在Android或肯定有人写了一些开源$ C ​​$ C,可以在您的活动的onCreate方法开始运行,这需要在一个XML文件,并设置您的布局?

I found some tutorials using SAXparser: http://www.androidpeople.com/android-xml-parsing-tutorial-using-saxparser/ http://twigstechtips.blogspot.com/2010/12/android-how-to-parse-xml-string.html and can imagine writing something from scratch that recognizes all the standard xml layout tags and then dynamically loads each part of the layout. But that's a lot of work to do from scratch! Surely this functionality is available in android, or surely someone has written some open source code which can be run at the start of your activity's onCreate method, which takes in an xml file and sets your layout?

我发现了一个类似的,但不能令人满意的回答的问题在这里: 如何创建一个布局文件编程的 这让我觉得,既然的setContentView必须采取一个整数RESOURCEID作为它的参数,但事实上,这些都是pre出炉在编译的时候可能是一个问题。 (的setContentView也可以采取一个视图对象作为参数,但我不想一吨的if语句和一个地传递每个视图对象之一,我想一些code表示输入数据的XML文件或XML字符串,设置内容视图。)

I found a similar but unsatisfactorily answered question here: How to create a layout file programmatically which makes me think that since setContentView must take an integer resourceID as its argument, the fact that these are pre-baked at compile time might be a problem. (setContentView may also take a View object as its argument, but I don't want a ton of if statements and to pass it each View object one by one, I want some code that inputs an xml file or xml string and sets the content view.)

也许我的方式偏离了轨道。是否有另一种方式做到这一点?我认为有一个与动态加载皮肤的应用程序的能力是非常重要的。

Maybe I'm way off track. Is there another way to do this? I would think that the ability to have an app with dynamically loaded skins is important.

谢谢!

推荐答案

我有类似的要求,并尝试了同样的做法 - 这是行不通的。

I had similar requirements and tried the same approach - it does not work.

文件明确指出这一点: http://developer.android.com/reference /android/view/LayoutInflater.html

Documentation clearly states this: http://developer.android.com/reference/android/view/LayoutInflater.html

更新:

由于OP需要在运行时加载创建的XML布局:

Since OP needs to load XML layouts created at runtime:

也许可以这样做,通过创建XML布局文件,将它们复制到虚拟项目,上设备创建.apk文件,然后加载APK。

Possibly this could be done, by creating XML layout files, copying them to dummy project, create .apk and then load apk on to device.

DexClassLoader 可以再用来加载类里面的apk。

DexClassLoader can be then used to load classes inside apk.