An explanation of XML tools properties in Android

Source: Internet
Author: User

First Part

In Android development, when writing layout code, the IDE can see the layout Preview.

However, some effects must be run before they can be seen, like this: TextView does not set any characters in the XML, but instead sets the text in the activity. So in order to preview the effect in the IDE, you must set the Android:text property for the TextView control in XML

1234567 <textview &NBSP;&NBSP; android:id= "@+id/text_main"    android:layout_width= "Match_parent" &NBSP;&NBSP; android:layout_height= "wrap_content" &NBSP;&NBSP; android:textappearance= &NBSP;&NBSP; android:layout_margin= "@dimen/main_margin" &NBSP;&NBSP; android:text= "I am a title"  />

In general, when we do this, we tell ourselves that it's okay, and then I'll delete all these things after I finish writing the code. But you may forget that there is such a code in your final product.

Use tools, don't do anything stupid.

The above situation can be avoided, and we use the tools namespace and its properties to solve this problem.

1 xmlns:tools="http://schemas.android.com/tools"

Tools can tell Android Studio which properties are ignored at run time and are only valid when designing the layout. For example, we want to make the Android:text property available only in the layout preview.

1234567 <textview &NBSP; android:id= "@+id/text_main " &NBSP; android:layout_width= "match_parent" &NBSP; android:layout_height= "wrap_content" &NBSP; android:textappearance= "@style/textappearance.title" &NBSP; android:layout_margin= "@dimen/main_margin" &NBSP; tools:text= "I am a title"  />

Tools can override all of Android's standard properties, and Android: Switch to Tools: Yes. While running, even tools: itself is ignored and will not be brought into the APK.

Types of tools Properties

The tools attribute can be divided into two types: one that affects Lint hints, and one that is about XML layout design. The above introduction is the most basic use of tools: in the UI design to cover the standard Android properties, belong to the second type. The lint related properties are described below.

Lint properties related to

123 tools:ignoretools:targetApitools:locale

Tools:ignore

The Ignore property is to tell Lint to ignore some warnings in XML.

Suppose we have such a imageview

1234567 <imageview &NBSP;&NBSP; android:layout_width= " Wrap_ Content " &NBSP;&NBSP; android:layout_height= "wrap_content" &NBSP;&NBSP; android:layout_marginstart= "@dimen/margin_main" &NBSP;&NBSP; android:layout_margintop= "@dimen/margin_main" &NBSP;&NBSP; android:scaletype= "center" &NBSP;&NBSP; android:src= "@drawable/divider"  />

Lint will indicate that the ImageView is missing the android:contentdescription attribute. We can use tools:ignore to ignore this warning:

12345678 <ImageView  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_marginStart="@dimen/margin_main"  android:layout_marginTop="@dimen/margin_main"  android:scaleType="center"  android:src="@drawable/divider"  tools:ignore="contentDescription"/>

Tools:targetapi

Suppose minsdklevel, and you used the controls in api21 such as rippledrawable

12 <ripple xmlns:android="http://schemas.android.com/apk/res/android"  android:color="@color/accent_color"/>

The lint will prompt a warning.

In order not to display this warning, you can:

1234 <ripple xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:color="@color/accent_color"  tools:targetApi="LOLLIPOP"/>

Tools:locale (native language) properties

By default res/values/ The string in the Strings.xml performs a spelling check, and if it is not English, it prompts for spelling errors, and the following code tells studio that the local language is not English and there is no hint.

12345678 <resources  xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  tools:locale="it">   <!-- Your strings go here --> </resources>

This article first describes the most basic uses of tools-overrides the properties of Android, and then describes the properties that ignore lint hints. In the next article, we'll continue to cover other properties about UI previews (not Android standard properties).

PS: About ignoring the properties of lint, if you do not want to know, it does not matter, because does not affect the compilation, generally I do not control these warnings.

Part II

In this section we will continue to introduce other properties about UI previews (not standard Android properties).

    • Tools:context

    • Tools:menu

    • Tools:actionbarnavmode

    • Tools:listitem/listheader/listfooter

    • Tools:showin

    • Tools:layout


Tools:context

The context property is actually called the activity property, and with this property, the IDE knows what theme to use when previewing the layout. He can also help find relevant files in the Java Code of Android Studio (Go torelatedfiles)

The value of this property is the full package name of the activity

123456789 <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:id="@+id/container"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical"  tools:context="com.android.example.MainActivity">  <!-- ... --></LinearLayout>


Tools:menu

Tells the IDE which menu to use in the preview window, which will be displayed on the root node of layout (Actionbar location).

In fact, the preview window is very smart, and if the layout is associated with an activity (referring to the Tools:context association above) it will automatically query the code in the relevant activity's Oncreateoptionsmenu method to display the menu. The Menu Property can override this default behavior.

You can also define multiple menu resources for the menu property , separated by commas between different menu resources.

1 tools:menu="menu_main,menu_edit"

If you do not want to display the menu in the preview chart:

1 tools:menu=""

Finally, it is important to note that this property does not work when the subject is Theme.appcompat .

Tools:actionbarnavmode

This property tells the display mode of the IDE app bar (actionbar in material), and its value can be

123 standardtabslist
123456 <linearlayout xmlns:android= " Http://schemas.android.com/apk/res/android " &NBSP;&NBSP;&NBSP;&NBSP; xmlns:tools= "Http://schemas.android.com/tools" &NBSP;&NBSP;&NBSP;&NBSP; android:orientation= "vertical" &NBSP;&NBSP;&NBSP;&NBSP; android:layout_width= "match_parent" &NBSP;&NBSP;&NBSP;&NBSP; android:layout_height= "match_parent" &NBSP;&NBSP;&NBSP;&NBSP; tools:actionbarnavmode= "tabs"  />

Similarly, when the subject is Theme.appcompat (r21+, at least) or theme.material, or uses the layout to include the toolbar method. This property also does not work, only the Holo theme is valid.

ListItem, Listheader, and Listfooter properties

As the name implies, add a preview layout of the head end and the child item in the preview effect such as ListView Expandablelistview.

1234567 <GridView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" tools:listheader="@layout/list_header" tools:listitem="@layout/list_item" tools:listfooter="@layout/list_footer"/>

Layout properties

Tools:layout tells Ide,fragment what to display when the program previews

123456789 <fragment xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/item_list"    android:name="com.example.fragmenttwopanel.ItemListFragment"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_marginLeft="16dp"    android:layout_marginRight="16dp"    tools:layout="@android:layout/list_content"/>

Tools:showin

This property is set on the root element of a layout that is <include> by another layout. This allows you to point to one of the layouts that contains this layout, which is rendered at design time with the surrounding external layout. This will allow you to "view and edit this layout in context." Requires Studio 0.5.8 or later.

An explanation of XML tools properties in Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.