Question:
What is the meaning of xmlns:tools in Android XML layout?
For example, in:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
...
Do I need to put it?
Answer:
It defines the XML namespace of the document. You should put it, otherwise tags like <RelativeLayout>
could be not recognied by the parser.
Namespaces are a way for XML documents to include tags from various vendors. By using xmlns
attribute you declare, that, by default, you're using XML elements defined here:http://schemas.android.com/apk/res/android (note that this link is broken - this discussion explains why).
You also declare additional namespace, tools
, which is not your default namespace, thus when referencing elements or attributes defined there, you must add tools
prefix, on example:
tools:context=".SomeActivity"
其实
xmlns:tools就是一个命名空间,代表后面以tools为前缀的(即tools:开头的)等同于之前设定的字符串。