Android Development Gadget: Tools Properties
Http://blog.chengyunfeng.com/?p=755#ixzz4apLZhfmi
Today to introduce some of the more useful Android development process, but we do not use small tools. These gadgets can improve the efficiency of Android app development and also improve code quality. So it is necessary to use it.
The Tools property in the layout file is described first.
If you create a simple sample project with Android Studio, there is a line in the resulting layout file:
Xmlns:tools= "Http://schemas.android.com/tools"
Some of the properties defined under the Tools namespace are the tools properties we're going to cover. As the name implies, these properties are prepared for editing tools, specifically Android Studio and the layout file compiler (AAPT). Use these properties to tell the editor some extra information when developing the program.
Tools:ignore
This property is prepared for Lint, and the value of the property is a comma-delimited Lint problem ID. Tell Lint that these problems do not need to be detected while the code is being detected. For example:
<string name= "Show_all_apps" tools:ignore= "Missingtranslation" >All</string>
Tools:targetapi
This property is also intended for Lint. Same as the Java annotations @TargetApi. The value can be either the Android version designator or the corresponding integer value. For example:
<gridlayout tools:targetapi= "Ice_cream_sandwich" >
Tools:locale
This property is used by both Lint and Android Studio. The values are language and region abbreviations. If you set this value to a non-English type, Studio does not perform a spell check. This property typically appears on the root element of the resource file. Used to tell Lint that the file should be used in that language environment. For example: Values/strings.xml file set this property to ES
<resources xmlns:tools= "Http://schemas.android.com/tools" tools:locale= "es" >
The settings above tell the tool that the default folder's resources are in Spanish instead of English.
Tools:context
This property is used by both Lint and Android Studio. Appears in the layout root element to tell the tool which Activity is associated with the layout file. This way, the layout editor uses the associated Activity's theme to render the layout file when it is designed. The value is the same as the activity name value in the manifests file. For example:
<android.support.v7.widget.gridlayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns:tools= "Http://schemas.android.com/tools" tools:context= ". Mainactivity "... >
Tools:layout
This property is used by Android Studio. It is usually used in the <fragment> element to tell the editor which layout file the fragment is using.
<fragment android:name= "com.example.master.ItemListFragment" tools:layout= "@android: Layout/list_content"/>
Tools:listitem/listheader/listfooter
Android Studio uses this property to render list elements. Can be used in Adapterview's child View. such as <ListView>, <GridView>, <ExpandableListView> and so on. This allows the editor to display the preview content with the specified content at design time.
<listview
Android:id= "@android: Id/list"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
tools:listitem= "@android:layout/simple_list_item_2″/>
Tools:showin
This property is used by Android Studio. Typically used in <include> layout files that are referenced by other layout files. Tells the editor that the layout file is used in a different layout file. For example
<?xml version= "1.0″encoding=" utf-8″?>
<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"/>
Tools:menu
This property is used by Android Studio. Use the root element of the layout file to tell the editor about the menu contents on the ActionBar. The value is the ID of the defined menu, with multiple IDs separated by commas, and the name of the XML file that defines the menu. For example:
<?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″/>
Tools:actionbarnavmode
This property is used by Android Studio. Use the root element of the layout file to tell the editor the navigation mode on the ActionBar. The values are "standard", "List", and "tabs" one.
<?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:actionbarnavmode= "Tabs"/>
In addition to these properties, standard Android layout properties can be used as tools properties. The layout editor chooses the layout file with these properties. These properties are called " Design-time layout Properties " And they are used only by the layout editor, which does not exist in the compiled code.
For example
<relativelayoutxmlnS: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:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=" @string/hello_world "Tools:text=" Hello World "Android:layout_width=" Wrap_content "Tools:layout_width=" 400DP "Tools:background=" @android: Color/holo_red_dark "android:layout_height=< Span class= "hljs-string" > "wrap_content"/></relativelayout> the above layout file, preview the diagram in the layout editor as follows: The advantage of this is that when you edit the layout file, You don't have to set some test values for the actual properties to preview the effect, and then when the program is published, the contents of these tests forget to delete. For example, to preview the display of EditText, you set a text to the Text property: <edittext android:text= "John Doe" android:layout_width= "wrap_content" Android:layout_height= "wrap_content"/>
当你发布项目的时候,却忘记了把该字符串给删除了。 如果这里面包含有比较机密的内容的话,你的秘密就被泄露出去了。 你可以用
Tools:text=
这样在发布的时候,这些内容会自动去掉。
设计时布局属性的限制:
- Only standard properties are currently supported, that is, properties under the Android namespace.
- The current code hints support is not very good, it is recommended to first use the Android namespace to edit the property, and then replace Android with tools.
- Design-time layout properties can only be used in layout files and cannot be used in other resource files.
- There are some questions to note about this category: https://code.google.com/p/android/issues/detail?id=46186
Android Development Gadget: Tools Properties (GO)