一个xmlns:android,这实际上是如何工作的?

时间:2022-08-25 17:26:14

Consider the following perfectly well formed layout file on android:

在android上考虑以下完美格式的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello World, PurpleActivity"/>
</LinearLayout>

I looked at this and got curious about that ULR: http://schemas.android.com/apk/res/android. So, I tried to enter it into my web browser's address bar and also tried to fetch it via CURL, but both methods showed that it doesn't actually exist. I looked for something akin to an apk/res/android directory in the android-sdk folder on my local machine, but that searched turned up nothing as well. So, I figured since this resource didn't actually exist, maybe that I could change the xmlns:android line to whatever I wanted. I went ahead and changed it to

我看了这个并对ULR感到好奇:http://schemas.android.com/apk/res/android。因此,我尝试将其输入到我的Web浏览器的地址栏中,并尝试通过CURL获取它,但两种方法都显示它实际上并不存在。我在本地机器上的android-sdk文件夹中寻找类似于apk / res / android目录的东西,但搜索结果也没什么。所以,我想,因为这个资源实际上并不存在,也许我可以将xmlns:android行更改为我想要的任何内容。我继续把它改成了

http://schemas.android.com/apk/res/androidASDFASDFASDFASDFLKJSDFLEIE

and then tried to recompile my project. I got this result:

然后尝试重新编译我的项目。我得到了这个结果:

-resource-src:
     [echo] Generating R.java / Manifest.java from the resources...
     [aapt]     (skipping hidden file '/Users/rutski/Desktop/purple/res/layout/.#main.xml')
     [aapt] /Users/rutski/Desktop/purple/res/layout/main.xml:2: error: No resource identifier found for attribute 'orientation' in package 'androidASDFASDFASDFASDFLKJSDFLEIE'
     [aapt] /Users/rutski/Desktop/purple/res/layout/main.xml:2: error: No resource identifier found for attribute 'layout_width' in package 'androidASDFASDFASDFASDFLKJSDFLEIE'
     [aapt] /Users/rutski/Desktop/purple/res/layout/main.xml:2: error: No resource identifier found for attribute 'layout_height' in package 'androidASDFASDFASDFASDFLKJSDFLEIE'
     [aapt] /Users/rutski/Desktop/purple/res/layout/main.xml:7: error: No resource identifier found for attribute 'layout_width' in package 'androidASDFASDFASDFASDFLKJSDFLEIE'
     [aapt] /Users/rutski/Desktop/purple/res/layout/main.xml:7: error: No resource identifier found for attribute 'layout_height' in package 'androidASDFASDFASDFASDFLKJSDFLEIE'
     [aapt] /Users/rutski/Desktop/purple/res/layout/main.xml:7: error: No resource identifier found for attribute 'text' in package 'androidASDFASDFASDFASDFLKJSDFLEIE'

This leaves me confused. If the resource doesn't exist anyway, then what does it matter if I change the xmlns:android value to some other nonexistent resource? And if it does exist, then where is it?

这让我感到困惑。如果资源不存在,那么如果我将xmlns:android值更改为其他一些不存在的资源有什么关系呢?如果确实存在,那么它在哪里?

2 个解决方案

#1


6  

xmlns:android="http://schemas.android.com/apk/res/android" declares an XML namespace prefix. xmlns indicates it's about a namespace declaration. android is the prefix. http://schemas.android.com/apk/res/android is a URI - a string that uniquely identifies which namespace is referred to. The URI can be something abstract (as in this case); it is not necessarily a URL that physically locates something.

xmlns:android =“http://schemas.android.com/apk/res/android”声明XML名称空间前缀。 xmlns表示它是关于命名空间声明的。 android是前缀。 http://schemas.android.com/apk/res/android是一个URI - 一个唯一标识所引用名称空间的字符串。 URI可以是抽象的东西(如本例所示);它不一定是物理定位的URL。

In general, you are free to choose the actual prefix. You can most likely replace android with something else and it will still work.

通常,您可以*选择实际前缀。你很可能用其他东西替换android,它仍然可以工作。

So what that attribute actually means is "We need to work with nodes from the namespace http://schemas.android.com/apk/res/android here, and in this file, we're going to refer to that namespace as android.

那么该属性实际意味着什么是“我们需要使用命名空间http://schemas.android.com/apk/res/android中的节点,在这个文件中,我们将把这个命名空间称为android 。

If, in several files, you define different namespace prefixes using the same URI, all those files will be referring to the same namespace - because the URI is the same. The prefix can be seen a a shorthand notation for referring to the actual namespace. The prefix can only be used inside the file that defines it.

如果在几个文件中使用相同的URI定义不同的名称空间前缀,则所有这些文件都将引用相同的名称空间 - 因为URI是相同的。前缀可以看作是引用实际命名空间的简写符号。前缀只能在定义它的文件中使用。

#2


0  

It's not a resource, it's just a URI. Usually, processes that work with XML files expect a specific XML namespace. When you use your namespace, it's as if you renamed the attributes to something else. And so the tools that work with them don't recognize them anymore.

它不是资源,它只是一个URI。通常,使用XML文件的进程需要特定的XML命名空间。当您使用命名空间时,就像将属性重命名为其他内容一样。因此,使用它们的工具不再能识别它们。

#1


6  

xmlns:android="http://schemas.android.com/apk/res/android" declares an XML namespace prefix. xmlns indicates it's about a namespace declaration. android is the prefix. http://schemas.android.com/apk/res/android is a URI - a string that uniquely identifies which namespace is referred to. The URI can be something abstract (as in this case); it is not necessarily a URL that physically locates something.

xmlns:android =“http://schemas.android.com/apk/res/android”声明XML名称空间前缀。 xmlns表示它是关于命名空间声明的。 android是前缀。 http://schemas.android.com/apk/res/android是一个URI - 一个唯一标识所引用名称空间的字符串。 URI可以是抽象的东西(如本例所示);它不一定是物理定位的URL。

In general, you are free to choose the actual prefix. You can most likely replace android with something else and it will still work.

通常,您可以*选择实际前缀。你很可能用其他东西替换android,它仍然可以工作。

So what that attribute actually means is "We need to work with nodes from the namespace http://schemas.android.com/apk/res/android here, and in this file, we're going to refer to that namespace as android.

那么该属性实际意味着什么是“我们需要使用命名空间http://schemas.android.com/apk/res/android中的节点,在这个文件中,我们将把这个命名空间称为android 。

If, in several files, you define different namespace prefixes using the same URI, all those files will be referring to the same namespace - because the URI is the same. The prefix can be seen a a shorthand notation for referring to the actual namespace. The prefix can only be used inside the file that defines it.

如果在几个文件中使用相同的URI定义不同的名称空间前缀,则所有这些文件都将引用相同的名称空间 - 因为URI是相同的。前缀可以看作是引用实际命名空间的简写符号。前缀只能在定义它的文件中使用。

#2


0  

It's not a resource, it's just a URI. Usually, processes that work with XML files expect a specific XML namespace. When you use your namespace, it's as if you renamed the attributes to something else. And so the tools that work with them don't recognize them anymore.

它不是资源,它只是一个URI。通常,使用XML文件的进程需要特定的XML命名空间。当您使用命名空间时,就像将属性重命名为其他内容一样。因此,使用它们的工具不再能识别它们。