我正在编写一个包含启动屏幕活动的库,我希望能够通过将 res/xml/ 资源传递给 AndroidManifest.xml 文件中该活动的元数据来配置它。
这就是我在库中加载 xml 资源的方式,但是属性计数始终为 -1。我知道 res 如果肯定指向正确的资源,因为我在其上调用了 getResourceName,它是正确的。
int res = metaData.getInt(METADATA_SPLASH, 0);
XmlResourceParser parser = getResources().getXml(res);
AttributeSet attrs = Xml.asAttributeSet(parser);
此外,我的应用程序中的 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>
最后,在我的库中,我在 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>
任何帮助将非常感激。