We know that during the Android development process, our data often comes from the server, only at runtime to get the data display, so in the layout XML writing process, because of the lack of data, we can not directly see the layout after filling the data, so what do you do at this time?

Often see some partners in the layout file to temporarily write some data to see the layout display effect, after viewing, and then manually delete these data.

Is it troublesome? Do we have a simpler solution?

Google has already considered the above problem, so it provides support for the Tools namespace in the development tool.

Use Tools: namespace added attributes in the layout XML file. Tools attributes are automatically removed by the build tool during the project build process and ultimately have no impact on the builder.

The Tools: namespace attribute provides a number of useful functions beyond the preview function for writing layouts, so let’s take a closer look at the tools attribute features.


Tools attributes are officially classified into three types:

Error handling Attributes

Lint is mainly used to help us control some error warnings generated by Lint.


####tools: Ignore works with any element

As we know, a Lint tool is provided in the Android development tools to help you easily identify and correct problematic and structure-quality code.

Lint has different ids for different problem warnings. This property allows You to set the corresponding problem ID in Lint to cause that warning to be ignored, or to set multiple problem ids to ignore multiple warnings using commas.

Ex. :

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

In the case of multi-language resource packs, lint will issue a MissingTranslation warning if a String in strings.xml is not translated in another language.

With tools:ignore=”MissingTranslation” we can tell Lint to ignore this warning for this String.

Another example is a more common problem in our development:

When using ImageView tag, without android: contentDescription lint would give contentDescription warnings,

At this point, we can ignore this warning by adding tools to the ImageView tag :ignore=”contentDescription”.

 <ImageView ...
  tools:ignore="contentDescription"
/>
Copy the code

####tools:targetApi

Applies to any element

This property is similar to the @targetAPI annotation we used in our code

Lint will issue a version warning when the lowest version of Android supported by the component element we are using is greater than minSdkVersion, the lowest version specified for our project.

This property tells Lint to ignore warnings by setting the corresponding API version for the specified layout.

Ex. :

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:targetApi="14" >
Copy the code

If we don’t use GridLayout under v4, then GridLayout will only support systems with API LEVEL 14 or higher, and if our project’s minSdkVersion is lower than 14, Lint will give a version warning

When tools:targetApi=”14″ is added, Lint will stop version warnings.


####tools:locale applies to

elements

This property is used to specify the locale used by the default resource file to prevent Lint from unnecessarily warning spell-checking.

By default, the development tool assumes that our language is English, so the word checker will generate warnings for certain English letters during its detection.

For example, if my app_name is called TRSDK, TRSDK will generate a wavy line warning by default:

If I use tools:locale to set the default environment to Zh, the warning will go away


## Design-time View Attributes

These properties are mainly for Android Layout features. You can set these properties to quickly preview the layout on the Layout preview screen of Android Studio.


#### Tools: Instead of android: (Instead of any Android: starting attribute)

Applies to the

class view element

You can preview property Settings in the layout preview screen by replacing the Android: prefix with tools: prefix for view component properties.

And when the code is built, the build tool automatically removes these properties without any impact on the final packaged APK.

Such as:

If the TextView data needs to be retrieved at runtime, we can preview the effect of the data filling in the layout preview screen by setting the value of Tool :text, as shown in the figure below:

We can set both the Android: property (displayed only at runtime) and the matching Tools: property (displayed only in the layout preview screen), and eventually the Tools property will be removed when building the project and displayed only as the Android: property value.

Suppose we have multiple sub-layouts in FrameLyout, and we only want to preview the appearance of a single layout. We can also preview the appearance of a single sub-layout by setting the Tools property:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="First" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Second"
    tools:visibility="invisible"  />
Copy the code

####tools:context

Applies to partial

root layouts

This property is used to set the Activity associated with the layout file so that code can be generated automatically associated with the Activity context when using the development tool’s Quick Fix.

Such as:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <Button
          android:text="Button"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/button"
          android:layout_weight="1"
          android:onClick="onButtonClicked"/>
</LinearLayout>
Copy the code

Here we set tools: Context = “.MainActivity” to the root LinearLayout. When we use Android Studio’s quick fix function, we will associate the related Activity with the prompt:


#### Tools: Layout for

This property is used to define the layout that needs to be drawn inside the Fragment to preview the fragment’s display view in the layout preview screen.

Such as:

<fragment android:name="com.example.master.ItemListFragment"
    tools:layout="@layout/list_content" />
Copy the code

####tools:listitem/tools: Listheader/tools: Listfooter works with subclasses of

and

As we know, the item layout of the list is set by code in the getView of the Adapter when we write the list. In a non-runtime environment, we cannot see the preview of the list directly.

You can view the layout preview screen of the development tool directly by setting the corresponding layout:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:listitem="@layout/sample_list_item"
    tools:listheader="@layout/sample_list_header"
    tools:listfooter="@layout/sample_list_footer" />
Copy the code

####tools:showIn applies to the layout root referenced by the

tag

Assuming that TextView is referenced in the Activity_main layout, If you use tools:showIn to point to Activity_main, you will see the TextView displayed in activity_Main in the layout preview screen.

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:showIn="@layout/activity_main" />
Copy the code

####tools:menu applies to partial roots

This property is used to specify the Menu layout that needs to be displayed in the App bar. You can specify multiple Menu layouts using comma separators.

<? xml version="1.0" encoding="utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:menu="menu1,menu2" />
Copy the code

If you know how this property works, please leave a comment in the comments.


### Resource Decreasing Attributes

This type property allows you to enable strict association detection

It also decides whether to keep or discard the specified resource file when the project is built.

Using this property requires setting shinkResources to true in build.gradle to enable resource compression.

When resource compression is enabled, resources that are not referenced in code or resource files are removed during the build process.

android {
    ...
    buildTypes {
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro'}}}Copy the code

####tools:shrinkMode applies to the < Resources > resource TAB

This property allows you to specify whether the build tool uses “safe Mode “security mode (this mode preserves all explicitly referenced Resources and may be used by [resources.getidentifier ()]. ](https://developer.android.google.cn/reference/android/content/res/Resources.html#getIdentifier(java.lang.String, Java.lang.String, java.lang.String)) dynamically referenced resources)

Or “strict mode” (this mode is reserved only for resources that are explicitly referenced in code or resource files)

ShrinkMode =”safe” by default, set tools to :shrinkMode=”strict” in.

<? xml version="1.0" encoding="utf-8"? > <resources xmlns:tools="http://schemas.android.com/tools"
    tools:shrinkMode="strict" />
Copy the code

####tools:keep applies to the

resource TAB

This property allows you to specify which resources to keep when using the Resource compression function.

Since resource compression is enabled, unreferenced resource files are removed during the build process, While some use [resources.getidentifier () ](https://developer.android.google.cn/reference/android/content/res/Resources.html#getIdentifier(java.lang.String, Java.lang.String, java.lang.String)) may be deleted by mistake.

We can use this property to specify which resources to keep and cannot be removed:

<? xml version="1.0" encoding="utf-8"? > <resources xmlns:tools="http://schemas.android.com/tools"
    tools:keep="@layout/used_1,@layout/used_2,@layout/*_3" />
Copy the code

####tools:discard applies to the < Resources > resource TAB

When using resource compression to remove unnecessary resource files, some resources are referenced but the removal does not have any effect on the app, or the Gradle Plugin incorrectly removes the associated resources.

At this point, we can use this property to specify the resource to be removed:

<? xml version="1.0" encoding="utf-8"? > <resources xmlns:tools="http://schemas.android.com/tools"
    tools:discard="@layout/unused_1" />
Copy the code