Android - When and for which purpose we use the fo

2019-08-30 22:48发布

问题:

I am new to android development and confused about the following prefixes:

  1. xmlns:android
  2. xmlns:app
  3. xmlns:tools
  4. android:something
  5. app:something
  6. tool:something

I am confused about them because sometimes I use one, and android studio marks it as red underline which means i should use another one, and when sometimes i use another one android studio marks it with red line which means i should use the other one and so on, which makes me confused. And even in some cases i noticed that we are not allowed to use any one of them for example style attribute, layout attribute etc.

What each represents?

Which one is used for which purpose?

Which one is the parent, child and sub child, or may be i am wrong about this logic?

Is there anymore than them? If yes please share the resource or link

Why sometimes we don't use these prefixes for example style, layout etc?

I want to know about them to use them with understandable and proper way. Thank you very much !!!

回答1:

as Henry said, these are called namespaces.

the tools namespace is used to make changes to components without it being applied at runtime. it is useful for designing and testing different layouts or components without it affecting your code when you run the app.

the android namespace is usually used for an android attribute, meaning that it comes from the Android SDK itself.

the app namespace is used for support library components, so that it works for various different versions of android.

xmlns is simply a namespace specifying that it is an xml document, these are used for making changes to the xml document itself.