简体   繁体   English

Android从元数据读取自定义xml资源

[英]Android read custom xml resources from meta-data

I am writing a library that contains a splash screen activity and I want to be able to configure it by passing an res/xml/ resource to the meta-data of that activity in the AndroidManifest.xml file. 我正在编写一个包含启动画面活动的库,并且我希望能够通过将res / xml /资源传递到AndroidManifest.xml文件中该活动的元数据来对其进行配置。

This is how I load the xml resource in the library, however the attribute count is always -1. 这是我在库中加载xml资源的方式,但是属性计数始终为-1。 I know that res if definately pointing to the correct resource as I called getResourceName on it, and it was correct. 我知道res如果一定要指向正确的资源,就像我在其上调用getResourceName一样,它是正确的。

int res = metaData.getInt(METADATA_SPLASH, 0);
XmlResourceParser parser = getResources().getXml(res);
AttributeSet attrs = Xml.asAttributeSet(parser);

Also, my xml resource in my application looks like this: 另外,我的应用程序中的xml资源如下所示:

<?xml version="1.0" encoding="utf-8"?>
<splash xmlns:lib="http://schemas.android.com/apk/res-auto"
    lib:caption="@string/copyright" lib:drawable="@drawable/logo_aperture"
    lib:activity=".ui.HomeActivity">
</splash>

And finally, in my library I have defined the attributes in an attrs.xml file as here: 最后,在我的库中,我在attrs.xml文件中定义了属性,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="drawable" format="reference"></attr>
    <attr name="caption" format="string"></attr>
    <attr name="activity" format="string"></attr>
</resources>

Any help would be much appreciated. 任何帮助将非常感激。

This is a late answer, but just in case it helps someone else. 这是一个较晚的答案,但以防万一它可以帮助别人。 Before trying to use the AttributeSet, insert the following block of code: 在尝试使用AttributeSet之前,请插入以下代码块:

int type;
while ((type=parser.next()) != XmlResourceParser.END_DOCUMENT
    && type != XmlResourceParser.START_TAG) {
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM