Android xmlns: tools usage, androidxmlns
In Android development, the layout file contains the following content:
<LinearLayoutxmlns: 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: context = ". MainActivity"
/>
What is the role of tools? Refer to document http://tools.android.com/tech-docs/tools-attributes
This document lists many tools methods, such as tools: ignore/tools: targetApi/tools: locale, but what we want to discuss today is the most detailed content in this document, Designtime Attributes, which is a http://tools.android.com/tips/layout-designtime-attributes.
These are attributes which are used when the layout is rendered inthe tool, but have no impact on the runtime. this is useful if you for examplewant to put sample data in your textfields for when you are editing the layout, but you don't want those attributes to affect your running app.
These attributes are used to render the layout without affecting the program running. That is to say, this attribute only appears during preview layout and does not exist when the program is running.
This description is very interesting. For example, when we want to display a piece of text in the layout file, the text content may change dynamically in the program, in particular, the content of the string with parameters such as % 1 $ s must be displayed in android: text before, then adjust the size and color parameters of the text, and then delete the android: text attribute. With the tools parameter, you can directly use tools: text to display the text during preview, saving the trouble of deleting it.
Commonly used tools: text, tools: visibility, tools. src, tools. background
Some restrictions are also listed in the official documents as follows:
- Currently only overriding existing attributes is supported. we may want to define some additional convenience attributes to make it simple to for example choose which child in a ViewFlipper to show etc. currently, only existing attributes of overriding are supported.
- You have to manually edit in your designtime attributes at this time
- They do not appear as an option in for example the layout editor property sheet.
- Editor code completion does not help you enter these attributes; the easiest way to use them is to first enter them in the Android namespace, and when done replacing the prefix. currently, this part can only be manually entered, and Automatic Code filling cannot be completed.
- Note that designtime attributes are supported only for layout files themselves. you cannot use them anywhere else -- in menu XML files, in string resource definitions, etc. only layout is supported. menu/resource/string is not supported currently. This statement conflicts with the content in http://tools.android.com/tech-docs/tools-attributes, and the tools-attributescontains <string name = "show_all_apps" tools: ignore = "MissingTranslation"> All </string> and <resources xmlns: tools = "http://schemas.android.com/tools" tools: locale = "es"> Contents
- Designtime attributes can only be used for framework resources, not custom attributes at this point. It can only be applied to the attributes provided by the system. custom attributes are not in this range.
- See https://code.google.com/p/android/issues/detail? Id = 46186 for background or additional requests or comments.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.