An explanation of XML tools properties in Android

Source: Internet
Author: User
Tags locale

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

< 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= "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.

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.

<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  = "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

    1. Tools:Ignore
    2. Tools:targetapi
    3. Tools:locale

Tools:ignore

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

Suppose we have such a imageview

 <  imageview  android:layout_width  = "Wrap_content"   = "Wrap_content"   = "@dimen/margin_main"   Android:layout_margintop  = "@dimen/margin_main"   Android:scaletype  = "Center"   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:

< 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

<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:

<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, the string in res/values/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.

< Resources   xmlns:android = "Http://schemas.android.com/apk/res/android"   xmlns:tools= "Http://schemas.android.com/tools"  tools:locale= "it"  >   <!-- -</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

<LinearLayoutxmlns: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

    1. Standard
    2. Tabs
    3. List
<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: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.

<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

<Fragmentxmlns: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.

The introduction of tools is done.

Reprinted from: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0309/2567.html

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.