第一部分
安卓开发中,在写布局代码的时候,ide可以看到布局的预览效果。
但是有些效果则必须在运行之后才能看见,比如这种情况: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命名空间以及其属性来解决这个问题。
1
|
xmlns:tools=
"http://schemas.android.com/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
|