Android - Designtime Layout Attributes & Tools Attributes
2015-06-29 14:41
726 查看
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" ... The tools namespace is a specially recognized namespace by the Android tools, so all the attributes you define on view elements in the tools-namespace will be automatically stripped when the application is packaged and there is no runtime overhead. 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 fastScrollAlwaysVisibleattribute 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=""/> Here's another example; we have a FrameLayout with multiple children, and at designtime we only want to see one of them, let's say the second child: We can use the tools:visibility attribute: <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 |
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/toolsand 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:ignoreThis 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: |
ref1:http://tools.android.com/tips/layout-designtime-attributes
ref2:http://tools.android.com/tech-docs/tools-attributes
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories