Android tools的一些知识

xmlns:tools=”http://schemas.android.com/tools

tools属性可以分为两种:一种是影响Lint提示的,一种是关于xml布局设计的。

Lint相关的属性:

1
2
3
tools:ignore //告诉Lint忽略xml中的某些警告。
tools:targetApi
tools:locale

我们常用的tools第二种属性:在UI设计的时候覆盖标准的android属性。

比如Android开发中在布局文件里面都会有如下面的内容:

1
2
3
4
5
6
7
<....
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
/>

其中tools起什么作用呢?可以参考文档http://tools.android.com/tech-docs/tools-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.

所以tools中第二种属性用于渲染布局,而不会影响到程序运行。也就是说只在预览布局时出现,在程序运行时相当于该属性不存在。比如上面例子中的,tools:context=”.MainActivity”,目的是让Layout Editor知道当前布局对应哪一个Activity,来更好的显示预览界面,假如当前使用了一个主题,或者使用了一个ActionBar,就都可以显示出来。如果不添加这个属性,就只会显示布局文件定义的界面。

tools:text VS android:text

转自:http://blog.csdn.net/m0_37222746/article/details/54340893
既然TextView有text属性,我们可以为它提供初始值,在应用运行前就知道它大概的样子。而当应用在设备中真正运行时,所有tools打头的属性都会被忽略,上面的文字不会显示出来。

例子如下:


注意最后一个属性。它只有在编辑预览的时候有效,运行的时候是根本就不会打入apk中的。