我在像Kitkat或Gingerbread这样的旧平台上玩新的矢量xml drawables。我有这样的图像:
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="300dp"
android:height="300dp"
android:viewportWidth="300"
android:viewportHeight="300">
<group>
<path
android:fillColor="@color/primary"
android:pathData="@string/svg_path_background" />
<path
android:fillColor="#fff"
android:pathData="@string/svg_path1" />
<path
android:fillColor="@color/primary"
android:pathData="@string/svg_path2" />
</group>
</vector>
确切的数值并不重要,在Lollipop上运行正常。但是我现在尝试读取这个xml文件(来自drawable目录)。这很好,除了在每个标签中都缺少一个属性。我不知道为什么会这样:
public static void load(Resources res, int drawable) {
XmlResourceParser vector = res.getXml(drawable);
int type = -1;
StringBuilder xml = new StringBuilder();
while(type != XmlResourceParser.END_DOCUMENT) {
try {
type = vector.getEventType();
switch(type) {
case XmlResourceParser.START_TAG:
xml.append("<");
xml.append(vector.getName());
for(int i = 0; i < vector.getAttributeCount(); i++) {
xml.append(" ");
xml.append(vector.getAttributeName(i));
xml.append("=\"");
xml.append(vector.getAttributeValue(i));
xml.append("\"");
}
xml.append(">\n");
break;
case XmlResourceParser.END_TAG:
xml.append("</");
xml.append(vector.getName());
xml.append(">\n");
break;
}
vector.next();
} catch(XmlPullParserException | IOException e) {
e.printStackTrace();
}
}
Log.d("XML-Test", xml.toString());
vector.close();
}
上面这段代码的输出就是Kitkat和Gingerbread:
<vector height="300.0dip" width="300.0dip" viewportHeight="300.0">
<group>
<path pathData="@2131624234">
</path>
<path pathData="@2131624248">
</path>
<path pathData="@2131624249">
</path>
</group>
</vector>
Lollipop上的正确输出:
<vector height="300.0dip" width="300.0dip" viewportWidth="300.0" viewportHeight="300.0">
<group>
<path fillColor="@2131296323" pathData="@2131624234">
</path>
<path fillColor="#ffffffff" pathData="@2131624248">
</path>
<path fillColor="@2131296323" pathData="@2131624249">
</path>
</group>
</vector>
这可能是构建系统的错误吗?我不知道不同的输出是如何可能的。我使用AS 1.0.2和buildToolsVersion 21.1.2