【发布时间】:2011-10-30 11:12:19
【问题描述】:
为什么xml布局文件需要这一行?
xmlns:android="http://schemas.android.com/apk/res/android"
【问题讨论】:
-
不需要在第一行吗?
标签: android xml android-activity layout xml-namespaces
为什么xml布局文件需要这一行?
xmlns:android="http://schemas.android.com/apk/res/android"
【问题讨论】:
标签: android xml android-activity layout xml-namespaces
在 XML 中,xmlns 声明了一个命名空间。事实上,当你这样做时:
<LinearLayout android:id>
</LinearLayout>
xml 将使用http://schemas.android.com/apk/res/android:id 来唯一标识,而不是调用android:id。通常这个页面不存在(它是一个 URI,而不是一个 URL),但有时它是一个解释使用的命名空间的 URL。
命名空间的用途与 Java 应用程序中的包名几乎相同。
Here 是一个解释。
统一资源标识符 (URI)
统一资源标识符 (URI) 是一个字符串 标识 Internet 资源。
最常见的 URI 是统一资源定位符 (URL) 标识 Internet 域地址。另一种不常见的类型 URI 是通用资源名称 (URN)。
在我们的示例中,我们将只使用 URL。
【讨论】:
android:layout_width 而不仅仅是layout_width?
要理解为什么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>
Uniform Resource Indicator(URI):
例如:http://schemas.android.com/apk/res/android:id 是这里的 URI
xmlns:android 描述了安卓
命名空间。textview 小部件,使用不同的
与 android textview 相比,android 命名空间有助于
区分我们自定义的textview 小部件和android
textview小部件【讨论】:
xmlns 指的是XML namespace
在 XML 中使用前缀时,必须为前缀定义一个所谓的命名空间。命名空间由元素开始标记中的 xmlns 属性定义。命名空间声明具有以下语法。 xmlns:prefix="URI"。
注意:解析器不使用命名空间 URI 来查找信息。
目的是给命名空间一个唯一的名字。但是,公司通常使用命名空间作为指向包含命名空间信息的网页的指针。
【讨论】:
这只是 XML 名称空间声明。我们使用这个命名空间来指定下面列出的属性属于 Android。因此它们以 "android:"
开头您实际上可以创建自己的自定义属性。因此,为了防止两个属性命名相同但行为不同的名称冲突,我们添加前缀“android:”来表示这些是 Android 属性。
因此,此命名空间声明必须包含在 XML 文件根视图的开始标记中。
【讨论】:
xmlns:android定义 Android 命名空间。此属性应始终设置为“
http://schemas.android.com/apk/res/android”。
参考https://developer.android.com/guide/topics/manifest/manifest-element#nspace
【讨论】:
通俗地说:
如果没有 xmlns:android=”http://schemas.android.com/apk/res/android” android 相关的标签将不会在我们布局的 xml 文档中被识别。
【讨论】:
在 XML 中,元素名称由开发人员定义。当试图混合来自不同 XML 应用程序的 XML 文档时,这通常会导致冲突。用户或 XML 应用程序将不知道如何处理这些差异。使用名称前缀可以轻松避免 XML 中的名称冲突。在 XML 中使用前缀时,必须定义前缀的命名空间。命名空间可以由元素开始标记中的 xmlns 属性定义。命名空间声明具有以下语法。 xmlns:prefix="URI"。
【讨论】:
xmlns:android="http://schemas.android.com/apk/res/android"
这是 xmlns:android ="@+/id" 的形式。现在引用它,我们使用例如
android:layout_width="wrap_content"
android:text="Hello World!"
另一个xmlns是
xmlns:app="http://schemas.android.com/apk/res-auto"
格式为 xmlns:app = "@+/id",用法如下
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
【讨论】:
我认为命名空间很清楚,因为我们可以创建自己的属性,如果用户指定的属性与Android相同,则可以避免命名空间的冲突。
【讨论】:
xmlns:android 这是在 Android 中定义 android 命名空间的开始标记。这是 android google 开发人员定义的标准约定。当您使用和布局默认或自定义时,必须使用此命名空间。
定义 Android 命名空间。此属性应始终设置为“
http://schemas.android.com/apk/res/android”。
【讨论】:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns : 是 xml 命名空间 网址:“http://schemas.android.com/apk/res/android”不过是
XSD 即 [XML 模式定义]:用于定义 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>
让我解释一下什么样的规则?。
此类规则在 XML XSD 中定义:“http://schemas.android.com/apk/res/android”
有点晚了,但我希望这对你有帮助。
【讨论】:
以上答案中缺少以下重点,
当我们在 xml 文件的根目录中声明 xmlns:android="http://schemas.android.com/apk/res/android" 时,所有已经附加到这个命名空间的属性和标签都会被导入.
所以下次我们给 android: 时,就会出现自动完成列表。
【讨论】:
这是一个 XML 命名空间声明,用于指定视图组中的属性与 android 相关。
【讨论】: