为什么这行代码xmlns:android="http://schemas.android.com/apk/res/android"必须是布局xml文件中的第一行?

181

在xml布局文件中,为什么需要这行代码?

xmlns:android="http://schemas.android.com/apk/res/android" 

它不需要在第一行吗? - Aada
13个回答

1
在上面的答案中缺少以下重要点:
当我们在xml文件的根部声明xmlns:android="http://schemas.android.com/apk/res/android"时,所有已附加到此命名空间的属性和标记将被导入。
因此,下次我们输入android:时,自动完成列表会出现。

1
xmlns:android="http://schemas.android.com/apk/res/android" 

xmlns:是 XML 命名空间,而URL:"http://schemas.android.com/apk/res/android" 意味着

XSD 是 [XML schema definition],用于定义 XML 文件的规则。

例如:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_marginBottom="4dp"
   android:hint="User Name"
  />
</LinearLayout> 

我来解释一下,“什么样的规则”是什么。
  1. 在上面的XML文件中,我们已经为我们的布局定义了layout_width,如果您第二次定义相同的属性,将会出现错误。
  2. 虽然已经有EditText,但如果您想添加另一个EditText也没有问题。
这种类型的规则在XML XSD中定义:“http://schemas.android.com/apk/res/android 有点晚了,但我希望这可以帮到你。

0

这是一个XML命名空间声明,用于指定在其内部声明的视图组中的属性与Android相关。


please elaborate it more - Pramod S. Nikam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接