Where are the schemas for XML files on an Android project?

后端 未结 4 2304
孤街浪徒
孤街浪徒 2020-12-05 09:32

Where are the schemas (DTD or XML schema) for the XML files used on Android like AndroidManifest.xml or the layouts?

4条回答
  •  失恋的感觉
    2020-12-05 10:06

    Been searching around the same subject to find out how android studio does the autocomplete & stuff in XML, and I too was hopeful to find some XSD or something, but:

    In Android we use a mixture of static and dynamic DOM definitions:

    1-Some files are defined using classes and annotations, see for example Manifest (note: to get correct information you should most likely use the merged manifest, but that's outside of the scope of this doc).

    2-Other information is read from resources, using naming conventions to find a styleable that contains attrs relevant to a given XML tag. For example if we recognize a tag as corresponding to a View subclass in a layout file (e.g. “TextView”), we find the corresponding styleable, look at the attrs it contains and register DOM extensions for the given tag that correspond to these attr resources. See AttributeProcessingUtil and SubtagsProcessingUtil for code that reads styleables and AndroidDomExtender for the extension that plugs into the DOM system.

    3-Sometimes the styleable is determined statically, but the attrs are read dynamically to stay up to date with the platform version used in the project. This is done using the @Styleable annotation.

    https://android.googlesource.com/platform/tools/adt/idea/+/refs/heads/mirror-goog-studio-master-dev/android/src/org/jetbrains/android/dom/README.md

    For example this is how a shape drawable XML is defined in source codes of android studio:

    @DefinesXml
    @Styleable("GradientDrawable")
    public interface Shape extends DrawableDomElement {
      @Styleable("DrawableCorners")
      List getCornerses();
    
      @Styleable("GradientDrawableGradient")
      List getGradients();
    
      @Styleable("GradientDrawablePadding")
      List getPaddings();
    
      @Styleable("GradientDrawableSize")
      List getSizes();
    
      @Styleable("GradientDrawableSolid")
      List getSolids();
    
      @Styleable("GradientDrawableStroke")
      List getStrokes();
    }
    

    The styleables (like GradientDrawablePadding) are defined in android's attrs.xml

    https://android.googlesource.com/platform/tools/adt/idea/+/refs/heads/mirror-goog-studio-master-dev/android/src/org/jetbrains/android/dom/drawable/Shape.java

提交回复
热议问题