【问题标题】:Regarding Android xmlns关于Android xmlns
【发布时间】:2013-10-01 22:41:20
【问题描述】:

当我们在 Android 中开发布局时,所有的 xml 文件都以

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

这有什么作用以及为什么在开发应用程序时我的 xmlns 指的是网络中的链接?

PS:这是一个新手问题

【问题讨论】:

    标签: java android xml


    【解决方案1】:

    xmlns 是 xml 命名空间

    命名空间由元素开始标记中的 xmlns 属性定义。命名空间声明具有以下语法。 xmlns:prefix="URI"。

    xmlns:android

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

    http://www.w3schools.com/xml/xml_namespaces.asp

    这是一个类似问题的列表

    What does "xmlns" in XML mean?

    Why this line xmlns:android="http://schemas.android.com/apk/res/android" must be the first in the layout xml file?

    【讨论】:

      【解决方案2】:

      这就像在 C++ 中定义一个命名空间,

      可以在这里找到一个很好的解释:

      Why this line xmlns:android="http://schemas.android.com/apk/res/android" must be the first in the layout xml file?

      【讨论】:

        【解决方案3】:

        如果您对视图使用自己的属性,则需要使用命名空间。

        这在您创建自己(或使用其他人)自定义视图时最常用。

        例如,如果您创建自己的自定义 textView 支持使用 assets 文件夹中的自定义字体,您可能需要添加以下内容:

        <...MyTextView app:fontFile="fonts/myFont.ttf" .../>
        

        基本上,如果您不使用 xmlns(ns: 名称空间) ,则在另一个 xml 文件中使用的标签之间可能会发生冲突。例如:

        文件1:

        <table>
          <name>jad</name>
        </table>
        

        文件2:

        <table>
          <tr>
            <td>google</td>
          </tr>
        </table>
        

        如果将这些 XML 片段加在一起,就会出现名称冲突。两者都包含一个元素,但元素的内容和含义不同。

        由于这个主要原因,使用了限定名称空间。还有其他几种用途。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-07-15
          • 2015-05-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多