https://www.jianshu.com/p/9f1d179ac358
二、常见的XML命名空间
常见的XML命名空间有:android、tools、app(自定义命名空间)。
1.android
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:xml namespace,声明要开始定义一个命名空间。
android:namespace-prefix,命名空间的名字。
http://schemas.android.com/apk/res/android:看起来是一个URL,但是这个地址是不可访问的,实际上这是一个URI(统一资源标识符)。
使用这行代码,我们就可以引用命名空间中的属性,如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="New Text"
android:id="@+id/textView" />
</LinearLayout>
在这个布局中,只要以android:开头的属性便是引用了命名空间中的属性,
android是赋予命名空间一个名字,就跟我们平时在定义变量一样,比如我把它取成zwm,那么上面的代码我们也可以写成:
<LinearLayout xmlns:zwm="http://schemas.android.com/apk/res/android"
zwm:layout_width="match_parent"
zwm:layout_height="match_parent" >
<TextView
zwm:layout_width="wrap_content"
zwm:layout_height="wrap_content"
zwm:layout_gravity="center"
zwm:text="New Text"
zwm:id="@+id/textView" />
</LinearLayout>
作者:TomyZhang
链接:https://www.jianshu.com/p/9f1d179ac358
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。