在Android UI开发中,我们可以通过IDE看到xml布局的预览效果,但是有些控件只有在运行后才能显示,比如TextView,我们只有在运行后才会填充数据,但是有些时候我们需要提前预览效果,便会经常性的写一些测试数据。比如:TextView的android:text=“滚犊子”。在开发完成后你如果记得把这个数据删掉还好,如果忘记了,你懂得....,还有就是xml中有一些警告,当然这些警告并不影响编译,但是对于一些有强迫症的人来说,还是很有杀伤力的。好了,说了这么多废话,只为能引出今天的主题:android tools

下面我们来看下Android tools的作用和使用方法

首先在使用的时候我们需要在xml中添加:

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


tools可以告诉我们的编译器,那些属性是针对布局设计的,在运行时是要被忽略的。tools可以覆盖Android的全部标准属性,把Android:换成tools:即可。运行时便会连同tools属性一起忽略掉,不会出现在apk中。比如:

<LinearLayout
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"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="滚犊子"/>
</LinearLayout>

这样你就不用担心你会忘记删除测试数据,可以安心发版了。


tools的属性可分为两种,一种是我们上面说的,覆盖Android标准属性,关于xml布局设计的。还有一种是关于lint提示的。


上面我们说的是布局设计的作用和用法,下面说关于Lint的用法


Lint相关的属性主要有三个:

tools:ignore=""
tools:targetApi=""
tools:locale=""


1、tools:ignore

这个属性的主要作用便是忽略xml中的某些警告,你比如我们在一个ImageView中,我们并不需要设置android:contentDescription属性,但是当你的ImageView没有设置这个属性时,警告便会出来恶心你,他不会影响的编译,只是会出来恶心你而已。使用方式:

<ImageView
android:id="@+id/navigation_item_images"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="contentDescription"
/>


2、tools:targetApi

这个属性主要是消除你使用了比你设置最低SDK版本高的控件时的警告,例如:当你设置的最低SDK版本:minSdkVersion=8,而你使用了api21的控件,此时便会有出现警告。使用方式为:

<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"
tools:targetApi="LOLLIPOP"
>

</FrameLayout>


3、tools:locale

这个属性作用在res/value/string.xml下,默认情况下res/values/strings.xml中的字符串会执行拼写检查,如果不是英语,会提示拼写错误,通过以下代码来告诉studio本地语言不是英语,就不会有提示了。使用方式:

<resources
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:locale="it"
>
</resources>


Lint的这些属性不了解也没问题的,则个属性就是为那些洁癖程序员设置的,因为即使你不去管它也不会影响你程序的运行。

这篇文章就到这里,后面我们会介绍Android:tools的非标准UI设计预览的属性。


更多相关文章

  1. Android本地视频播放器开发--搜索本地视频(2)
  2. Android的Adapter用法总结
  3. 关于GridView宽高的问题(转载rain的文章)
  4. Android(安卓)布局简要范例
  5. Android(安卓)组件系列-----Activity初步
  6. android 的布局单位 dip dp sp px总结
  7. 设置不同的android:inputTye属性时,如果需要修改EditText hint的
  8. 关于不需要添加android:debuggable属性就可以进行apk的调试一些
  9. android,闹钟定时功能,实现过程

随机推荐

  1. uni-app 跳转Android原生界面(Activity),
  2. Android 文件操作,删除,拷贝文件等
  3. Android Interface Definition Language
  4. layer-list使用
  5. 「Android」SurfaceFlinger分析
  6. Android常遇疑难问题
  7. 转发:bitmap 设置图片尺寸,避免 内存溢出 O
  8. Android - 永不锁屏,开机不锁屏,删除设置中
  9. Android在listview添加checkbox实现单选
  10. 部署Android SDK 和Eclipse 插件