android中xml tools属性详解—从入门到Easy

一、说在前面的话

在使用Android Studio创建xml布局时,通常在自动生成代码时,会自动新增一个tools命名空间: xmlns:tools="http://schemas.android.com/tools,对于不了解tools怎么使用的同学,可能就直接忽略了或者直接就删除了。那今天就来讲讲它的作用,它有何种魅力能让Android Studio内置到xml模板中,那就让我们开始吧

二、Tools作用

在开发布局代码时,IDE可以实时看到预览的效果,但是有些效果则必须运行之后才能看到,例如:TextView在xml中没有设置任何字符,而是在Activity中动态设置的text,因此为了在调试布局时在IDE中看到预览效果,就必须在xml中为TextView控件设置android:text属性:

<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="这是验证布局" />

一般我们在这样做时会告诉自己,在发版之前一定会把这些测试数据一并删除了。但是,有时你在忙其他事情时可以把这件事抛出脑后,以至于老板在使用产品时无意中看到了测试数据,那你就要被吊起来了。

讲到这里时,你可能有点不耐烦了,说了一大堆还没有说tools的作用。那是因为上面讲述的是我们开发过程中经常遇到的痛点,那怎么解决这个痛点呢?这时就要讲述的tools的作用了。

根据官方文档描述,根据其属性的功能类型,大致可分为三类:

  • xml中的错误提醒处理
  • xml开发环境预览
  • 资源压缩

说的通俗一点就是:

  • 减少或者避免黄线提示,让代码更清爽,让编译少报错(有强迫症的人对这一点应该特别喜爱)
  • 让开发布局过程中更灵活,可以随心所欲的定制预览视图
  • 压缩资源文件,降低APK体积

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

<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="这是验证布局"/>

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

三、Tools使用

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

1. 影响Lint提示相关的属性
tools:ignore
tools:targetApi
tools:locale
  • tools:ignore
    假设我们有这样的一个ImageView
<ImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginStart="@dimen/margin_main"
  android:layout_marginTop="@dimen/margin_main"
  android:scaleType="center"
  android:src="@drawable/divider" />

Lint会提示该ImageView缺少android:contentDescription属性。我们可以使用tools:ignore来忽略这个警告:

<ImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginStart="@dimen/margin_main"
  android:layout_marginTop="@dimen/margin_main"
  android:scaleType="center"
  android:src="@drawable/divider"
  tools:ignore="contentDescription" />
  • tools:targetApi
    假设minSdkLevel 15,而你使用了api21中的控件比如RippleDrawable
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:color="@color/colorPrimaryDark"/>

则Lint会提醒警告。
为了不显示这个警告,可以:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        tools:targetApi="LOLLIPOP"
        android:color="@color/colorPrimaryDark"/>
  • tools:locale(本地语言)属性
    默认情况下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>

这篇文章首先介绍了tools的最基本用法-覆盖android的属性,然后介绍了忽略Lint提示的属性。

2.关于xml开发布局设计
  • tools:context
    context属性其实正是的称呼是activity属性,有了这个属性,ide就知道在预览布局的时候该采用什么样的主题。同时他还可以在android studio的java代码中帮助找到相关的文件:
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.android.example.MainActivity"> 
  ...
</LinearLayout>
  • listitem, listheader 和listfooter 属性
    顾名思义就是在ListView ExpandableListView等的预览效果中添加头部 尾部 以及子item的预览布局。
<GridView
 android:id="@+id/list"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 tools:listheader="@layout/list_header"
 tools:listitem="@layout/list_item"
 tools:listfooter="@layout/list_footer" />
  • layout属性
    tools:layout告诉ide,Fragment在程序预览的时候该显示成什么样
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_list"
    android:name="com.example.fragmenttwopanel.ItemListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    tools:layout="@android:layout/list_content" />

在这里插入图片描述

  • tools:showIn
    该属性设置于一个被其他布局的布局的根元素上。这让您可以指向包含此布局的其中一个布局,在设计时这个被包含的布局会带着周围的外部布局被渲染。这将允许您“在上下文中”查看和编辑这个布局。需要 Studio 0.5.8 或更高版本。

  • “@tools:sample/*” 占位资源
    该属性允许您将占位符数据或图像注入视图。 例如,如果要测试布局对文本的行为方式,但尚未为应用程序确定最终的UI文本,则可以使用占位符文本,如下所示:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   tools:text="@tools:sample/lorem" />

下表描述了可以注入布局的占位符数据的类型。

Attribute valueDescription of placeholder data
@tools:sample/full_namesFull names that are randomly generated from the combination of @tools:sample/first_names 和 @tools:sample/last_names.
@tools:sample/first_namesCommon first names.
@tools:sample/last_namesCommon last names.
@tools:sample/citiesNames of cities from across the world.
@tools:sample/us_zipcodesRandomly generated US zipcodes.
@tools:sample/us_phonesRandomly generated phone numbers with the following format: (800) 555-xxxx.
@tools:sample/loremPlaceholder text that is derived from Latin.
@tools:sample/date/day_of_weekRandomized dates and times for the specified format.
@tools:sample/date/ddmmyy
@tools:sample/date/mmddyy
@tools:sample/date/hhmm
@tools:sample/date/hhmmss
@tools:sample/avatarsVector drawables that you can use as profile avatars.
@tools:sample/backgrounds/scenicImages that you can use as backgrounds.

参考资源:官网地址

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值