您的位置:首页 > 移动开发 > Android开发

android xml tools 介绍(一)

2016-03-31 00:00 429 查看
摘要: android tools 属性主要分为两种,一种关于UI设计预览的属性,一种是消除警告提示的Lint属性

在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设计预览的属性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android tools studio