Android Tools 属性介绍

xml 布局文件在 Android 开发中是必不可以少的一部分,开发者对里面 android 前缀的属性非常熟悉,android:layout_widthandroid:layout_heightandroid:orientation 。这里要介绍的是另外一种一个非常实用且容易被忽略的 tools 属性。

新创建 xml 布局文件的时候,你可能会发现在布局根元素里默认添加了 tools 相关的属性 xmlns:toolstools:context

<android.support.constraint.ConstraintLayout 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:context="com.mvp.viewdemo.TestActivity">

</android.support.constraint.ConstraintLayout>

如果删掉 tools 相关的属性的时候,并不会引起编译问题跟布局预览问题,也不会影响代码的执行。那么这个属性到底有什么用?好,先挖个坑,下面再填坑。

tools 相关的属性大致分为三类:

  • 解决指定的 Lint 的警告提示
  • 强化 xml 布局预览属性
  • 资源缩减属性。

解决指定的 Lint 的警告提示

解决lint警告提示的 tools 属性有 tools:ignoretools:targetApitools:locale,分别用于忽略指定的 lint 异常警告提示,申明控件的运行系统版本,申明资源文件的语言条件。

tools:ignore

用于忽略指定的 lint 异常警告提示。我们经常会用 Android studio 里面的功能 Analyze -> Inspect Code 检查项目中可能存在的问题,但是有些警告只是官方规范建议,并不是真的存在潜在bug,如果要解决这类问题可以使用tools:ignore 来忽略一些异常警告提示。

比如经常出现的 Image without contentDescription 异常警告

这里写图片描述

这是因为 ImageView 属性少加了 android:contentDescription 但是实际开发中一般很少去添加这个属性。在布局的根元素添加 xmlns:tools="http://schemas.android.com/tools" tools:ignore="ContentDescription",便可以忽略这个警告提示

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="ContentDescription"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mvp.viewdemo.TestActivity"

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值