skip to Main Content

I begin to study Android development. I’ confused about the XML meaning for the UI design. Such as the following code, what the keyword xmlns, android, tools, app mean ? Are they some objects? For example, android:layout_width means that the property of the object 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>

2

Answers


  1. ns doesn’t stand for ‘notation’ here, It’s ‘namespace’ XMLNS

    All attributes of XML belongs to a namespace, for identify/qualify the name of an attribute, we have to declared their namespace’s.

    Login or Signup to reply.
  2. As someone answered, ns is a namespace. You can actually call them anything- if you wanted to use "foobar:" instead of "android:" you can, you’d just change xmlns:app to xmls:foobar.

    Now there are conventions most people follow, to make code more readable. "android:" is the stuff built into the framework. "app:" are attributes created by your app (or any library you use) and are generated at compile time. "tools:" are meant for things the IDE reads for display, such as default values for preview. Keeping to these names will make it easier for others to read your code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search