什么是“应用程序"Android XML 命名空间?
这是我从 res/menu/main.xml 文件中看到的 app 命名空间的示例
Here is an example of the app namespace that I've seen from a res/menu/main.xml file
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" >
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
app 命名空间有什么用途?它是标准"Android XML 命名空间吗?对于放置在两个不同命名空间(例如 app:showAsAction 和 android:showAsAction)中的相同属性,是否可以使用相同的值选项.
What purpose does the app namespace serve? Is it a "standard" Android XML namespace? Are the same value options available for the same attribute placed in two different namespaces (e.g. app:showAsAction and android:showAsAction).
来自文档:android:showAsAction=["ifRoom" |从不" |"withText" |总是" |"collapseActionView"]
即,如果属性改为:
android:showAsAction="never"
它几乎看起来像是某种子类化"机制,但我似乎无法从 Google/Android 来源中找到有关 app 命名空间的任何真实文档.
It almost looks like it might be some sort of "subclassing" mechanism, but I can't seem to find any real documentation on the app namespace from Google/Android sources.
app 命名空间并非特定于某个库,而是用于您的应用中定义的所有属性,无论是由您的代码或通过您导入的库,有效地为自定义属性创建一个全局命名空间 - 即不是由 android 系统定义的属性.
The app namespace is not specific to a library, but it is used for all attributes defined in your app, whether by your code or by libraries you import, effectively making a single global namespace for custom attributes - i.e., attributes not defined by the android system.
在这种情况下,appcompat-v7 库使用镜像 android: 命名空间的自定义属性来支持先前版本的 android(例如:android:showAsAction 仅在 API11 中添加,但 app:showAsAction(作为应用程序的一部分提供)适用于您的应用程序执行的所有 API 级别) - 显然使用 android:showAsAction 不适用于未定义该属性的 API 级别.
In this case, the appcompat-v7 library uses custom attributes mirroring the android: namespace ones to support prior versions of android (for example: android:showAsAction was only added in API11, but app:showAsAction (being provided as part of your application) works on all API levels your app does) - obviously using the android:showAsAction wouldn't work on API levels where that attribute is not defined.