定义及使用
xmlns是XML Namespaces的缩写,中文名称是XML(标准通用标记语言的子集)命名空间
自定义View的时候有时候会在布局文件中使用到
命名规则如下:
xmlns:前缀=http://shemas.android.com/apk/res/应用程序包名
例如:
xmlns:myview="http://schemas.android.com/apk/myview"
使用的时候按如下格式:
namespace-prefix(前缀):属性
例如:
myview:attr
示例
如果使用xmlns,则xmlns的定义必须放在最外层开始的的标记中
当命名空间被定义之后,所有带有相同前缀的子元素都会与同一个命名空间相关联。避免XML解析器对xml解析时的发送名字冲突,这就是使用xmlns的必要性。当自定义的View有自己的属性的时候,就用到xmlns来定义一个命名空间
例如自定义的一个环形进度条属性的命名空间:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:zp="http://schemas.android.com/apk/circlepb"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.zp.pbd.view.CircleWithNumber
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="50dip"
android:padding="5dp"
android:progress="70"
zp:progress_reached_bar_height="20dp"
zp:progress_text_color="#000000"
zp:progress_unreached_color="#ffffff" />
</LinearLayout>
定义并使用了一个zp命名空间