什么是“应用程序"Android XML 命名空间? [英] What is the 'app' Android XML namespace?

查看:27
本文介绍了什么是“应用程序"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:showAsActionandroid: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.

这篇关于什么是“应用程序"Android XML 命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆