android xml tools 介绍(一)

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

下面咱们来看下Android tools的做用和使用方法 程序员

首先在使用的时候咱们须要在xml中添加: api

     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> spa

 这样你就不用担忧你会忘记删除测试数据,能够安心发版了。 设计


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


上面咱们说的是布局设计的做用和用法,下面说关于Lint的用法 xml


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

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


一、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"
    />


二、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>


三、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设计预览的属性。 

相关文章
相关标签/搜索