xmlns xml命名空间在android 上面的应用

来源:互联网 发布:淘宝个体营业执照办理 编辑:程序博客网 时间:2024/06/06 19:08

xmlns,就是xmls name space 的简称,

1: 为什么要有这个xmlns呢? 为了区别不同类的相同名字的属性,避免冲突,参考本文最下面的摘录的内容,或者这个链接


2: android 中的xmlns 有android, app,fresco,tools等

<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:fresco="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.kodulf.viewpager.MainActivity">

2.1 首先是android,因为有了这个命名空间,我们才可以使用anroid:id,anroid:layout_width这些

2.2 app,这个是例如我们使用了v4包中的TabLayout了,这个时候TabLayout有它自己的一些属性,那么可以直接app:

<android.support.design.widget.TabLayout              android:id="@+id/tablayout_video"              android:layout_width="match_parent"              android:layout_height="@dimen/tablayout_height"              app:tabBackground="@drawable/tablayout_background"              app:tabIndicatorHeight="0dp"              app:tabSelectedTextColor="@color/white"              app:tabTextAppearance="@style/TabLayoutTextStyle"              app:tabTextColor="@color/white" />  


2.3 还有fresco,这个是我们导入了第三方的fresco的包以后,如果想要使用fresco自定义的一些属性的时候,需要首先导入这个命名空间,然后才能使用

https://www.fresco-cn.org/docs/getting-started.html

在xml布局文件中, 加入命名空间:<!-- 其他元素--><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:fresco="http://schemas.android.com/apk/res-auto"    android:layout_height="match_parent"    android:layout_width="match_parent">加入SimpleDraweeView:<com.facebook.drawee.view.SimpleDraweeView    android:id="@+id/my_image_view"    android:layout_width="130dp"    android:layout_height="130dp"    fresco:placeholderImage="@drawable/my_drawable"  />

2.4 然后是tools 这个:主要是xml 使用的时候的工具,具体的可以参考:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0309/2567.html


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



xmlns的介绍:链接

XML 命名空间提供避免元素命名冲突的方法。

命名冲突

在 XML 中,元素名称是由开发者定义的,当两个不同的文档使用相同的元素名时,就会发生命名冲突。

这个 XML 文档携带着某个表格中的信息:

<table>   <tr>   <td>Apples</td>   <td>Bananas</td>   </tr></table>

这个 XML 文档携带有关桌子的信息(一件家具):

<table>   <name>African Coffee Table</name>   <width>80</width>   <length>120</length></table>

假如这两个 XML 文档被一起使用,由于两个文档都包含带有不同内容和定义的 <table> 元素,就会发生命名冲突。

XML 解析器无法确定如何处理这类冲突。

使用前缀来避免命名冲突

此文档带有某个表格中的信息:

<h:table>   <h:tr>   <h:td>Apples</h:td>   <h:td>Bananas</h:td>   </h:tr></h:table>

此 XML 文档携带着有关一件家具的信息:

<f:table>   <f:name>African Coffee Table</f:name>   <f:width>80</f:width>   <f:length>120</f:length></f:table>

现在,命名冲突不存在了,这是由于两个文档都使用了不同的名称来命名它们的 <table> 元素 (<h:table> 和 <f:table>)。

通过使用前缀,我们创建了两种不同类型的 <table> 元素。

使用命名空间(Namespaces)

这个 XML 文档携带着某个表格中的信息:

<h:table xmlns:h="http://www.w3.org/TR/html4/">   <h:tr>   <h:td>Apples</h:td>   <h:td>Bananas</h:td>   </h:tr></h:table>

此 XML 文档携带着有关一件家具的信息:

<f:table xmlns:f="http://www.w3school.com.cn/furniture">   <f:name>African Coffee Table</f:name>   <f:width>80</f:width>   <f:length>120</f:length></f:table>

与仅仅使用前缀不同,我们为 <table> 标签添加了一个 xmlns 属性,这样就为前缀赋予了一个与某个命名空间相关联的限定名称。

XML Namespace (xmlns) 属性

XML 命名空间属性被放置于元素的开始标签之中,并使用以下的语法:

xmlns:namespace-prefix="namespaceURI"

当命名空间被定义在元素的开始标签中时,所有带有相同前缀的子元素都会与同一个命名空间相关联。

注释:用于标示命名空间的地址不会被解析器用于查找信息。其惟一的作用是赋予命名空间一个惟一的名称。不过,很多公司常常会作为指针来使用命名空间指向实际存在的网页,这个网页包含关于命名空间的信息。

请访问 http://www.w3.org/TR/html4/。



1 0
原创粉丝点击