A xmlns:android, how does that actually work?
Consider the following perfectly well formed layout file on 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
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'
开发者_StackOverflow [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="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.
In general, you are free to choose the actual prefix. You can most likely replace android
with something else and it will still work.
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
.
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.
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.
精彩评论