1. 程式人生 > 其它 >xml名稱空間的作用類似於java中包名所起的限定範圍的作用

xml名稱空間的作用類似於java中包名所起的限定範圍的作用

技術標籤:JAVA

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
來源:簡書
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。