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

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

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

在XML中,xmlns声明了一个Namespace。 其实,当你这样做的时候:

 <LinearLayout android:id> </LinearLayout> 

而不是调用android:id ,xml将使用http://schemas.android.com/apk/res/android:id是唯一的。; 通常这个页面不存在(这是一个URI,而不是一个URL),但有时它是一个URL,解释使用的名称空间。

命名空间与Java应用程序中的包名具有几乎相同的用途。

这是一个解释。

统一资源标识符(URI)

统一资源标识符(URI)是标识Internet资源的string。

最常见的URI是标识Internet域地址的统一资源定位符(URL)。 另一种不常用的URI是通用资源名称(URN)。

在我们的例子中,我们只会使用url。

xmlns引用XML名称空间

在XML中使用前缀时,必须定义前缀的所谓名称空间。 名称空间由元素的开始标记中的xmlns属性定义。 命名空间声明具有以下语法。 的xmlns:前缀= “URI”。

注意 :parsing器不使用名称空间URI来查找信息。

目的是为命名空间提供一个唯一的名称。 但是,公司经常使用名称空间作为指向包含名称空间信息的网页的指针。

要理解为什么xmlns:android=“http://schemas.android.com/apk/res/android”必须是布局xml文件中的第一个我们将使用示例来理解组件

Sample ::

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/container" > </FrameLayout> 

统一资源指标(URI)

  • 在计算中,统一资源标识符(URI)是用于标识资源名称的string。
  • 这种识别使得能够使用特定的协议在networking(通常是万维网)上与资源的表示进行交互。

例如: http://schemas.android.com/apk/res/android:id : http://schemas.android.com/apk/res/android:id : http://schemas.android.com/apk/res/android:id是这里的URI


XML命名空间

  • XML名称空间用于在XML文档中提供唯一命名的元素和属性。 xmlns:android描述了android命名空间。
  • 它的使用是这样的,因为这是谷歌devise的select,在编译时处理错误。
  • 还假设我们编写自己的textview小部件与android textview相比有不同的function,android命名空间有助于区分我们的自定义textview小部件和android textview小部件

xmlns:android定义Android命名空间。 此属性应始终设置为“http://schemas.android.com/apk/res/android”。;

请参阅http://developer.android.com/guide/topics/manifest/manifest-element.html

这只是XML名称空间声明。 我们使用这个名称空间来指定下面列出的属性属于Android。 因此,他们从“ android: ”开始

你实际上可以创build自己的自定义属性。 因此,为了防止两个属性被命名为相同的东西,但行为不同的名称冲突,我们添加前缀“ android: ”来表示这些是Android属性。

因此,这个名称空间声明必须包含在XML文件的根视图的开始标记中。

xmlns:android这是Android中定义android命名空间的开始标记。 这是Android谷歌开发人员定义的标准约定。 当您使用和布局默认或客户,则必须使用此命名空间。

定义Android命名空间。 此属性应始终设置为“ http://schemas.android.com/apk/res/android ”。

必须阅读: http : //developer.android.com/guide/topics/manifest/manifest-element.html

在XML中,元素名称是由开发人员定义的。 当尝试混合来自不同XML应用程序的XML文档时,通常会导致冲突。 用户或XML应用程序将不知道如何处理这些差异。 使用名称前缀可以轻松避免XML中的名称冲突。 在XML中使用前缀时,必须定义前缀的名称空间。名称空间可以由元素的开始标记中的xmlns属性定义。名称空间声明具有以下语法。 的xmlns:前缀= “URI”。

  • Xmlns表示xml命名空间。
  • 它是为了避免xml中的命名冲突而创build的。
  • 为了避免以任何其他方式命名冲突,我们需要为每个元素提供一个前缀。
  • 为了避免在每个xml标签中重复使用前缀,我们在xml的根目录下使用xmlns。 因此,我们有标签xmlns:android =“ http://schemas.android.com/apk/res/android
  • 现在android这里只是意味着我们正在分配的命名空间“ http://schemas.android.com/apk/res/android ”。
  • 这个命名空间不是一个URL,而是一个URI,也被称为URN(通用资源名称),它很less用来代替URI。
  • 由于这个android将负责识别xml文件中的android相关元素,这将是android:xxxxxxx等。没有这个名字空间android:xxxxxxx将不被识别。

放置外行的话:

没有xmlns:android =“ http://schemas.android.com/apk/res/android ”android相关标签不会在我们的布局的xml文档中被识别。

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

这是xmlns的forms:android =“@ + / id”。 现在来引用它,我们使用例如

 android:layout_width="wrap_content" android:text="Hello World!" 

另一个xmlns是

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

它的forms是xmlns:app =“@ + / id”,其用法如下

  app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" 

这是一个XML名称空间声明,以便指定视图组中与其相关的属性是与android相关的。

我认为这清楚地说明了命名空间,因为我们可以创build自己的属性,并且如果用户指定的属性与Android相同,就避免了命名空间的冲突。