Android 项目中 XML 文件的架构在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/605325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where are the schemas for XML files on an Android project?
提问by pupeno
Where are the schemas (DTD or XML schema) for the XML files used on Android like AndroidManifest.xml or the layouts?
在 Android 上使用的 XML 文件(如 AndroidManifest.xml 或布局)的架构(DTD 或 XML 架构)在哪里?
采纳答案by Will
回答by Prof. Solymosi
Generally, defining a namespace in XML doesn't has to be a real existing URL but just a world wide unique String (therefore one prefers using their own URLs). Of course, it's nice if this URL contains the XML-schema (or worse, DTD). It would be also very nice if someone would create Android Ressource Schemata. I could help him as a bachelor thesis in CS. - Prof. Solymosi, Berlin
通常,在 XML 中定义命名空间不必是真实存在的 URL,而只是一个全球唯一的字符串(因此人们更喜欢使用自己的 URL)。当然,如果此 URL 包含 XML 模式(或更糟的是,DTD),那就太好了。如果有人能创建 Android Ressource Schemata 也很好。我可以帮助他作为 CS 的学士学位论文。- Solymosi 教授,柏林
回答by Paul
The XML schema doesn't seem to be documented, but there is a useful list of all the layout objects and their permitted attributes here:
XML 模式似乎没有记录,但是这里有一个有用的所有布局对象及其允许属性的列表:
http://developer.android.com/reference/android/R.styleable.html#lfields
http://developer.android.com/reference/android/R.styleable.html#lfields
回答by navid
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:
一直在搜索同一主题以了解 android studio 如何在 XML 中执行自动完成和内容,我也希望找到一些 XSD 或其他东西,但是:
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.
在 Android 中,我们混合使用静态和动态 DOM 定义:
1-一些文件是使用类和注释定义的,例如参见清单(注意:要获得正确的信息,您很可能应该使用合并的清单,但这超出了本文档的范围)。
2-从资源中读取其他信息,使用命名约定来查找包含与给定 XML 标记相关的属性的样式。例如,如果我们将一个标签识别为对应于布局文件中的 View 子类(例如“TextView”),我们会找到相应的样式,查看它包含的 attrs 并为给定的标签注册与这些 attr 资源相对应的 DOM 扩展. 请参阅 AttributeProcessingUtil 和 SubtagsProcessingUtil 以获取读取样式的代码和 AndroidDomExtender 以获取插入 DOM 系统的扩展。
3-有时样式是静态确定的,但动态读取属性以与项目中使用的平台版本保持同步。这是使用@Styleable 注释完成的。
For example this is how a shape drawable XML is defined in source codes of android studio:
例如,这是在 android studio 的源代码中定义形状可绘制 XML 的方式:
@DefinesXml
@Styleable("GradientDrawable")
public interface Shape extends DrawableDomElement {
@Styleable("DrawableCorners")
List<DrawableDomElement> getCornerses();
@Styleable("GradientDrawableGradient")
List<DrawableDomElement> getGradients();
@Styleable("GradientDrawablePadding")
List<DrawableDomElement> getPaddings();
@Styleable("GradientDrawableSize")
List<DrawableDomElement> getSizes();
@Styleable("GradientDrawableSolid")
List<DrawableDomElement> getSolids();
@Styleable("GradientDrawableStroke")
List<DrawableDomElement> getStrokes();
}
The styleables (like GradientDrawablePadding) are defined in android's attrs.xml
样式(如 GradientDrawablePadding)在 android 的 attrs.xml 中定义