从另一个APK加载资源(布局) [英] Load resource (layout) from another apk
问题描述
我想知道是否可以从另一个apk加载布局xml文件.我知道如何访问另一个应用程序资源,但是我不确定如何加载xml(如果可能的话).
I am wondering if it is possible to load a layout xml file from another apk. I know how to get access to another applications resources, but i am unsure how to load an xml if its even possible.
如果我想使用版式充气工具将该版式设置为视图,是否必须解析xml?
Would I have to parse the xml if I wanted to set that layout as a view using layout inflater?
按以下步骤排序...
Sort of like these steps...
- 使用XmlPullParser.setInput来加载" XML文件
- 将其转换为AttributeSet
- 从此AttributeSet创建视图
- 在活动"中使用setContentView(View)
我猜我在问什么,这有可能吗?就像我说的,我知道如何访问资源.但是我将如何访问xml布局?
I guess what i am asking, is this even possible? Like I said, i know how to access resources. But how would i access the layout xml?
谢谢!
推荐答案
我设法拉出了版面,并将其添加到我的viewflipper中,但是在这里它被加载为空白.
I managed to pull the layouts, and i add it to my viewflipper, however, there it is loaded as blank.
Resources packageResources;
Context packageContext;
try
{
packageResources = pm.getResourcesForApplication(packageName);
packageContext = this.createPackageContext(packageName, Context.CONTEXT_INCLUDE_CODE + Context.CONTEXT_IGNORE_SECURITY);
}
catch(NameNotFoundException excep)
{
// the package does not exist. move on to see if another exists.
}
Class layoutClass;
try
{
// using reflection to get the layout class inside the R class of the package
layoutClass = packageContext.getClassLoader().loadClass(packageName + ".R$layout");
}
catch (ClassNotFoundException excep1)
{
// Less chances that class won't be there.
}
for( Field layoutID : layoutClass.getFields() )
{
try
{
int id = layoutID.getInt(layoutClass);
XmlResourceParser xmlResourceLayout = packageResources.getLayout(id);
View v = new View(this, Xml.asAttributeSet(xmlResourceLayout));
this.viewFlipper.addView(v);
}
catch (Exception excep)
{
continue;
}
}
我没有任何错误,并且我进行了调试和检查.布局ID是正确的.但是,在我的viewFlipper中,它只是空白.我找不到警告或错误.
I get no errors, and i debugged and checked. The layout IDs are correct. However, in my viewFlipper its just blank. No warnings or errors i can find.
最终得到了解决方案...已将其张贴在下面,
Finally got the solution... Have posted it below,
这篇关于从另一个APK加载资源(布局)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!