android, app, tools in the Android layout file xml

In Android development, we often see things like xmlns:android, xmlns:app, and xmlns:tools in xml layout files, like the following;

<LinearLayout
    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"
              
     <ImageView
        android:id="@+id/head_portrait"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        app:showAsAction="never"
        tools:srcCompat="@drawable/head" />

</LinearLayout>

So what is the use and difference between them?

First of all, xmlns is the abbreviation of xml namespace, which means xml namespace.

  1. xmlns: The Android namespace is used to access and use attributes provided by the Android platform. The android:id, android:layout_width, android:layout_height used above are attributes provided by the Android platform.

  2. The xmlns:app (or xmlns:'customname') namespace is used to access custom attributes defined at the application scope. The app namespace is not specific to the library, it is used for all properties defined in the application, whether you pass the code or through the imported library, it is a global namespace that can be used for your custom properties, that is, android undefined Attributes.

  3. xmlns:tools tools can tell Android Studio which attributes are ignored at runtime and only valid when designing the layout. tools can cover all the standard attributes of android, just replace android: with tools:; at the same time, even tools: itself is ignored when running, and will not be brought into the apk.

    In Android development, when writing the layout code, the IDE can see the preview effect of the layout. But some effects can only be seen after running, such as this situation: TextView does not set any characters in xml, but sets text in activity. So in order to preview the effect in the ide, you must set the android:text attribute for the TextView control in the xml. In order to reduce the workload and development burden, the tools:text auxiliary attribute is used here.


reference:

Do you really understand android, app, tools in xml
What is the 'app' Android XML namespace
Android layout file xmlns:tools function and usage

Guess you like

Origin blog.csdn.net/qq_33697094/article/details/131122979