当前位置:   article > 正文

android中xml tools属性详解

android studio xml文件中的 tools

第一部分

安卓开发中,在写布局代码的时候,ide可以看到布局的预览效果。

01175340_wcLU.png

但是有些效果则必须在运行之后才能看见,比如这种情况:TextView在xml中没有设置任何字符,而是在activity中设置了text。因此为了在ide中预览效果,你必须在xml中为TextView控件设置android:text属性

1
2
3
4
5
6
7
<TextView
   android:id= "@+id/text_main"
   android:layout_width= "match_parent"
   android:layout_height= "wrap_content"
   android:textAppearance= "@style/TextAppearance.Title"
   android:layout_margin= "@dimen/main_margin"
   android:text= "I am a title"  />

一般我们在这样做的时候都告诉自己,没关系,等写完代码我就把这些东西一并删了。但是你可能会忘,以至于在你的最终产品中也会有这样的代码。

用tools吧,别做傻事

以上的情况是可以避免的,我们使用tools命名空间以及其属性来解决这个问题。

tools可以告诉Android Studio,哪些属性在运行的时候是被忽略的,只在设计布局的时候有效。比如我们要让android:text属性只在布局预览中有效可以这样

1
2
3
4
5
6
7
<TextView
  android:id= "@+id/text_main"
  android:layout_width= "match_parent"
  android:layout_height= "wrap_content"
  android:textAppearance= "@style/TextAppearance.Title"
  android:layout_margin= "@dimen/main_margin"
  tools:text= "I am a title"  />

tools可以覆盖android的所有标准属性,将android:换成tools:即可。同时在运行的时候就连tools:本身都是被忽略的,不会被带进apk中。

tools属性的种类

tools属性可以分为两种:一种是影响Lint提示的,一种是关于xml布局设计的。以上介绍的是tools的最基本用法:在UI设计的时候覆盖标准的android属性,属于第二种。下面介绍Lint相关的属性。

Lint相关的属性

1
2
3
tools:ignore
tools:targetApi
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/943998
推荐阅读
相关标签
  

闽ICP备14008679号