命名空间:如何知道如何以及何时使用它们?即:xmlns:andorid、xmlns:app、xmlns:tools

Namespaces: how to know how and when to use them? i.e.: xmlns:andorid, xmlns:app, xmlns:tools

什么是xmlns:androidxmlns:appxmlns:tools,它们之间的基本区别是什么?我应该什么时候使用它?

对于 android & app 命名空间,使用 this link.

it is used for all attributes defined in your app, whether by your code or by libraries you import, effectively making a single global namespace for custom attributes - i.e., attributes not defined by the android system.

In this case, the appcompat-v7 library uses custom attributes mirroring the android: namespace ones to support prior versions of android (for example: android:showAsAction was only added in API11, but app:showAsAction (being provided as part of your application) works on all API levels your app does) - obviously using the android:showAsAction wouldn't work on API levels where that attribute is not defined.

关于tools

Android Studio supports a variety of XML attributes in the tools namespace that enable design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources). When you build your app, the build tools remove these attributes so there is no effect on your APK size or runtime behavior.

tools 命名空间还有其他用法,您可以在字符串等中看到它们,例如:

<string name="show_all_apps" tools:ignore="MissingTranslation">All</string>

这有助于忽略错误。

此外,还有另一个有助于定义 TargetedAPI:

tools:targetApi

还有更多。

阅读: https://developer.android.com/studio/write/tool-attributes