Detailed explanation of xmlns:tools auxiliary attributes in android

When developing an Android project, xmlns:tools is mainly used in the compilation phase, and the compiler will automatically remove the tools-related attributes when packaging. Specifically, he is responsible for having the function:

  • ①Design preview
  • ②Constrained coding rules
  • ③ Response to compiler checks

1. Design preview

1. General property preview

In Android development, when writing layout code, IDE can see the preview effect of layout. But some effects can only be seen after running, such as this case: TextView does not set any characters in xml, but sets text in activity. So in order to preview the effect in ide, you have to set android:text attribute for TextView control in xml

<TextView
  android:id="@+id/text_main"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:textAppearance="@style/TextAppearance.Title"
  android:layout_margin="@dimen/main_margin"
  android:text="我是一个好人" />

In order to reduce the workload and development burden, the xmlns:tools auxiliary attribute is used here

<TextView
    android:id="@+id/text_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="@style/TextAppearance.Title"
    android:layout_margin="@dimen/main_margin"
    tools:text="我是一个好人" />

Similar to other Android properties, we only need to replace the android namespace with the tools namespace during the design phase.

2, page fragment combination preview

In the traditional development process, to preview the effect of a fragment in the whole, we generally use include, of course, we can also use tools:include. However, among many clips, to focus on a certain clip, we recommend the following method.

main page

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

fragment page

tools:showIn="@layout/activity_main"

 

Of course the common

tools:listitem

tools:listheader

tools:listfooter

tools:menu

2. Constraint coding rules

tools:context is mainly used to constrain the theme. When the theme of the Activity in our Manifest is switched, tools:context reversely adjusts the preview effect of the theme.

In addition, it can also detect whether the method corresponding to android:onClick exists.

 

3. Dealing with Compiler Checks

The compiler checks for non-compliant encoding, but these non-compliances are not errors, but non-conformances.

  • tools:ignore
  • tools:targetApi
  • tools:locale

tools:ignore

Suitable for: any element 
for: Lint check 
This attribute accepts a comma-separated lint issue id to ignore this element. For example, you can tell tools to ignore MissingTranslation errors:

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

tools:targetApi

Suitable for: any element 
for: Lint check 
it works the same as @TargetApi. Just used in xml. 
For example, GridLayout is only supported above API level 14:

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

Of course, you can directly use android.support.v7.widget.GridLayout in the support v7 library

tools:locale

Suitable for: <resources> 
for: Lint checking, Studio editor 
In <resources>, to avoid spell checker warnings, you can use tools:locale. Its value must be a valid locale qualifier 
. For example, in values/strings.xml, it means that the language used by the default strings is Spanish, not English:

<resources xmlns:tools="http://schemas.android.com/tools"
    tools:locale="es">

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325206888&siteId=291194637