内嵌复杂的 XML 资源
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
某些资源类型是由 XML 文件表示的多个复杂资源合成的。例如动画矢量可绘制对象就是封装矢量可绘制对象和动画的可绘制资源。这需要使用至少三个 XML 文件,如以下示例所示。
res/drawable/avd.xml
-
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/vectordrawable" >
<target
android:name="rotationGroup"
android:animation="@anim/rotation" />
</animated-vector>
res/drawable/vectordrawable.xml
-
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="64dp"
android:width="64dp"
android:viewportHeight="600"
android:viewportWidth="600" >
<group
android:name="rotationGroup"
android:pivotX="300.0"
android:pivotY="300.0"
android:rotation="45.0" >
<path
android:fillColor="#000000"
android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
</group>
</vector>
res/anim/rotation.xml
-
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/android"
android:duration="6000"
android:propertyName="rotation"
android:valueFrom="0"
android:valueTo="360" />
如果该矢量可绘制对象和动画会在别处重复使用,这是实现动画矢量可绘制对象的最佳方式。但是,如果这些文件仅用于此动画矢量可绘制对象,则可以通过更紧凑的方式来实现它们。
使用 AAPT 的内嵌资源格式,您可在同一 XML 文件中定义所有这三种资源,如以下示例所示。对于动画矢量可绘制对象,将文件放在 res/drawable/
下。
res/drawable/avd.xml
-
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt" >
<aapt:attr name="android:drawable" >
<vector
android:height="64dp"
android:width="64dp"
android:viewportHeight="600"
android:viewportWidth="600" >
<group
android:name="rotationGroup"
android:pivotX="300.0"
android:pivotY="300.0"
android:rotation="45.0" >
<path
android:fillColor="#000000"
android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
</group>
</vector>
</aapt:attr>
<target android:name="rotationGroup">
<aapt:attr name="android:animation" >
<objectAnimator
android:duration="6000"
android:propertyName="rotation"
android:valueFrom="0"
android:valueTo="360" />
</aapt:attr>
</target>
</animated-vector>
XML 标记 <aapt:attr >
告知 AAPT 将标记的子标记视为资源,并将其提取到自己的资源文件中。属性名称中的值用于指定在父标记内使用内嵌资源的位置。
AAPT 会为所有内嵌资源生成资源文件和名称。使用此内嵌格式构建的应用可与所有版本的 Android 兼容。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Inline complex XML resources\n\nCertain resource types are a composition of multiple complex resources represented by XML files.\nOne example is an animated vector drawable, which is a drawable resource encapsulating a vector\ndrawable and an animation. This requires the use of at least three XML files, as shown in the\nfollowing\nexamples.\n\n`res/drawable/avd.xml`\n:\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003canimated-vector xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:drawable=\"@drawable/vectordrawable\" \u003e\n \u003ctarget\n android:name=\"rotationGroup\"\n android:animation=\"@anim/rotation\" /\u003e\n \u003c/animated-vector\u003e\n ```\n\n`res/drawable/vectordrawable.xml`\n:\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cvector xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:height=\"64dp\"\n android:width=\"64dp\"\n android:viewportHeight=\"600\"\n android:viewportWidth=\"600\" \u003e\n \u003cgroup\n android:name=\"rotationGroup\"\n android:pivotX=\"300.0\"\n android:pivotY=\"300.0\"\n android:rotation=\"45.0\" \u003e\n \u003cpath\n android:fillColor=\"#000000\"\n android:pathData=\"M300,70 l 0,-70 70,70 0,0 -70,70z\" /\u003e\n \u003c/group\u003e\n \u003c/vector\u003e\n ```\n\n`res/anim/rotation.xml`\n:\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cobjectAnimator xmlns:android=\"http://schemas.android.com/apk/android\"\n android:duration=\"6000\"\n android:propertyName=\"rotation\"\n android:valueFrom=\"0\"\n android:valueTo=\"360\" /\u003e\n ```\n\n\nIf the vector drawable and animations are re-used elsewhere, this is the best way to implement an\nanimated vector drawable. But if these files are only used for this animated vector drawable,\nthen there is a more compact way to implement them.\n\nUsing AAPT's inline resource format, you can define all three resources in the same XML file, as\nshown in the following example.\nFor an animated vector drawable, put the file under `res/drawable/`.\n\n`res/drawable/avd.xml`\n:\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003canimated-vector xmlns:android=\"http://schemas.android.com/apk/res/android\"\n xmlns:aapt=\"http://schemas.android.com/aapt\" \u003e\n\n \u003caapt:attr name=\"android:drawable\" \u003e\n \u003cvector\n android:height=\"64dp\"\n android:width=\"64dp\"\n android:viewportHeight=\"600\"\n android:viewportWidth=\"600\" \u003e\n \u003cgroup\n android:name=\"rotationGroup\"\n android:pivotX=\"300.0\"\n android:pivotY=\"300.0\"\n android:rotation=\"45.0\" \u003e\n \u003cpath\n android:fillColor=\"#000000\"\n android:pathData=\"M300,70 l 0,-70 70,70 0,0 -70,70z\" /\u003e\n \u003c/group\u003e\n \u003c/vector\u003e\n \u003c/aapt:attr\u003e\n\n \u003ctarget android:name=\"rotationGroup\"\u003e\n \u003caapt:attr name=\"android:animation\" \u003e\n \u003cobjectAnimator\n android:duration=\"6000\"\n android:propertyName=\"rotation\"\n android:valueFrom=\"0\"\n android:valueTo=\"360\" /\u003e\n \u003c/aapt:attr\u003e\n \u003c/target\u003e\n \u003c/animated-vector\u003e\n ```\n\nThe XML tag `\u003caapt:attr \u003e` tells AAPT to treat the tag's child as a\nresource and extract it into its own resource file. The value in the attribute name specifies where\nto use the inline resource within the parent tag.\n\nAAPT generates resource files and names for all the inline resources.\nApplications built using this inline format are compatible with all versions of Android."]]