android xmlns:tools用法

来源:互联网 发布:谁是最可爱的人 知乎 编辑:程序博客网 时间:2024/05/16 14:48

在官方文档中有说这么一句:
          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.

这些属性用于渲染布局,而不会影响到程序运行。也就是说只在预览布局时出现,在程序运行时相当于该属性不存在。

这个说明非常有意思,比如我们在布局文件中想要显示一段文字时,而该文字内容在程序中可能动态变化,特别是有参数的字符串内容%1$s之类的内容,以前必须使用android:text显示,然后调整这段文字的大小颜色参数,然后完成后删除android:text属性。有了tools参数后,可以直接使用tools:text在预览时显示文字即可,省却了上面删除的麻烦。