Ressources XML complexes intégrées
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Certains types de ressources sont constitués de plusieurs ressources complexes représentées par des fichiers XML.
Par exemple, un drawable vectoriel animé associe un drawable vectoriel et une animation. Cela nécessite l'utilisation d'au moins trois fichiers XML, comme indiqué dans les exemples suivants.
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" />
Si le drawable vectoriel et les animations sont réutilisés ailleurs, c'est le meilleur moyen d'implémenter un drawable vectoriel animé. Toutefois, si ces fichiers ne sont utilisés que pour ce drawable vectoriel animé, il existe une manière plus compacte de les implémenter.
En utilisant le format de ressource intégré AAPT, vous pouvez définir les trois ressources dans le même fichier XML, comme illustré dans l'exemple suivant.
Pour un drawable vectoriel animé, placez le fichier sous 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>
La balise XML <aapt:attr >
indique à l'AAPT de traiter l'élément enfant de la balise comme une ressource et de l'extraire dans son propre fichier de ressources. La valeur du nom de l'attribut indique où utiliser la ressource intégrée dans la balise parente.
L'AAPT génère les fichiers de ressources et le nom de toutes les ressources intégrées.
Les applications créées avec ce format intégré sont compatibles avec toutes les versions d'Android.
Le contenu et les exemples de code de cette page sont soumis aux licences décrites dans la Licence de contenu. Java et OpenJDK sont des marques ou des marques déposées d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/07/27 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Il n'y a pas l'information dont j'ai besoin","missingTheInformationINeed","thumb-down"],["Trop compliqué/Trop d'étapes","tooComplicatedTooManySteps","thumb-down"],["Obsolète","outOfDate","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Mauvais exemple/Erreur de code","samplesCodeIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/07/27 (UTC)."],[],[],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."]]