Designtime Layout Attributes
As of Android Studio 0.2.11, the layout rendering (used in both the layout editor as well as the XML editor layout preview window), supports
designtime layout attributes.
These are attributes which are used when the layout is rendered in the tool, but have no impact on the runtime. This is useful if you for example want 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.
To use designtime attributes, first make sure you have the tools namespace defined in your layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
...
Then, to for example set the text of a text field, use the same attribute as you would from the Android framework, but use the
tools: namespace rather than the
android: namespace:
<TextView
android:text="Name:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
tools:text="John Doe"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
In the above, the Name label is using the normal text attribute, and will be shown at runtime. However, the text field is using a designtime attribute, so it will appear in the tool, but not at runtime.
In general, you can set any Android framework attribute as a designtime attribute; just
use the
tools:
namespace rather than the
android:
namespace. Note also that you don't have to choose either/or; you can set both the Android namespace attribute (which will be used at runtime) and a tools attribute (which will override the runtime attribute at designtime only).
You can also use designtime attributes to unset an attribute while in the tools. For example, there is a bug (
http://b.android.com/58448)
that you cannot use the
fastScrollAlwaysVisible attribute on ListViews in the layout editor. However, you may still want that attribute set at runtime. With designtime attributes you can work around it like this:
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fastScrollAlwaysVisible="true"
tools:fastScrollAlwaysVisible=""/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First"
tools:visibility="invisible" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second"
tools:visibility="visible" />
(Instead of
|
Tools Attributes
Android has a dedicated XML namespace intended for tools to be able to record information in XML files, and have that information stripped when the application is packaged such that there is no runtime or download size penalty.
The namespace URI is
http://schemas.android.com/tools
and is usually bound to the
tools:
prefix:
<FrameLayout xmlns: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" >
....
This document records our current uses of tools attributes. (
NOTE: These may change over time.)
tools:ignore
This attribute can be set on any XML element, and is a comma separated list of lint issue ID's that should be ignored on this element or any of its children, recursively.
<string name="show_all_apps" tools:ignore="MissingTranslation">All</string>
Used by: Linttools:targetApiThis attribute is like the @TargetApi annotation in Java classes: it lets you specify an API level, either as an integer or as code name, that this element is known to be running on. <GridLayout
tools:targetApi="ICE_CREAM_SANDWICH" >
Used by: Linttools:localeThis attribute can be set on the root element in a resource value file and should correspond to a language and optionally a region. This will let tools know what language (locale) the strings in the file are assumed to be. For example,
|