This article is translated from the official technical document Androd designtime Layout Attributes ": Http://tools.android.com/tips/layout-designtime-attributes.
This article address: http://blog.csdn.net/maosidiaoxian/article/details/41510581. Reprint please indicate the source. Please correct me if there is any inappropriateness in the translation.
Design-time Layout PropertiesIn the Android Studio 0.2.11 Release, layout rendering (for layout Editor and Layout Preview window for XML editor), supports
Design-time Layout Properties.
These properties are used when the layout is rendered in the tool, but have no effect on the runtime. This is useful, for example, if you want to edit the layout
The sample data is placed in a text box, but these properties do not affect the app you are running.
To use design-time properties, first make sure that you have a tool 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"
...
This tool namespace is a specially recognized namespace for Android tools, so all of the tool-namespace properties that you define on the view elements are automatically stripped when the application is packaged and do not cost the runtime.
Then, for example, you set the text field to use the same properties as the Android frame, but use the
tools:
namespace instead of
android:
Namespaces:
<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 example above, the Name tag uses the normal Text property, which will be displayed at run time. However, the text field uses design-time properties, so it appears in the tool instead of at run time.
In general, you can set the properties of any Android frame as a design-time property;Use
tools:
namespace instead of
android:
namespace. Also note that you do not have to select only one of them; you can set
two properties, Android namespace properties (used at run time), and tool properties (which will overwrite runtime properties at design time).
You can also use design-time properties to
delete a property's value in the tool. For example, there is a bug (http://b.android.com/58448), you cannot use the ListView in the Layout editor
fastScrollAlwaysVisible
property. However, you may still want the property to be set at run time. With design-time properties, you can solve this:
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fastScrollAlwaysVisible="true"
tools:fastScrollAlwaysVisible=""/>
Here is another example: we have a framelayout with multiple sub-view, and at design time we only want to see one of them, for example, the second one, we can use the Tools:visibility property:
<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" />
(depending on your needs, you may use
visibility="gone"
them instead
visibility="invisible"
.) )Limit
- Overwriting existing properties is currently supported only. We may want to define some additional handy attributes that make it easy to select sub-view, such as the viewflipper display.
- At this point you must manually edit your design-time properties
- They do not appear as an option, such as in the Layout Editor property sheet.
- The editor's code completion does not help you enter these properties; The simplest way to use them is to first enter in the Android namespace and then replace the prefix to complete.
- It is important to note that design-time properties only support the layout file itself. You can't use them anywhere else-for example, in a menu XML file, in a string resource definition, and so on.
- At this point, design-time properties can be used only in the framework resource, not in custom properties.
- View https://code.google.com/p/android/issues/detail?id=46186 For information such as background information, additional requests, or comments.
About
tools:
For more information about other properties of the namespace, see Tools Attributes.
Android official technical document translation-design-time layout properties