我開始學習Android開發。我對 UI 設計的 XML 含義感到困惑。比如下面的代碼,關鍵字xmlns, android, tools,app是什么意思?它們是一些物體嗎?例如,android:layout_width表示物件的屬性android。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
uj5u.com熱心網友回復:
正如有人回答的那樣, ns 是一個命名空間。實際上,您可以將它們稱為任何名稱——如果您想使用“foobar:”而不是“android:”,您只需將 xmlns:app 更改為 xmls:foobar。
現在大多數人都遵循一些約定,以使代碼更具可讀性。“android:”是框架中內置的東西。“app:” 是由您的應用程式(或您使用的任何庫)創建的屬性,并在編譯時生成。“工具:”用于 IDE 讀取以顯示的內容,例如預覽的默認值。保留這些名稱將使其他人更容易閱讀您的代碼。
uj5u.com熱心網友回復:
ns 在這里不代表“符號”,它是“命名空間” XMLNS
XML 的所有屬性都屬于一個命名空間,為了識別/限定屬性的名稱,我們必須宣告它們的命名空間。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/489256.html
