简体   繁体   中英

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.

This is how I load the xml resource in the library, however the attribute count is always -1. I know that res if definately pointing to the correct resource as I called getResourceName on it, and it was correct.

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 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:

<?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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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