终于建了一个自己个人小站:https://huangtianyu.gitee.io,以后优先更新小站博客,欢迎进站,O(∩_∩)O~~

第一部分

安卓开发中,在写布局代码的时候,ide可以看到布局的预览效果。

但是有些效果则必须在运行之后才能看见,比如这种情况:TextView在xml中没有设置任何字符,而是在activity中设置了text。因此为了在ide中预览效果,你必须在xml中为TextView控件设置android:text属性

<TextViewandroid: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="I am a title" />

一般我们在这样做的时候都告诉自己,没关系,等写完代码我就把这些东西一并删了。但是你可能会忘,以至于在你的最终产品中也会有这样的代码。

用tools吧,别做傻事

以上的情况是可以避免的,我们使用tools命名空间以及其属性来解决这个问题。

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

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

<TextViewandroid: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="I am a title" />

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

tools属性的种类

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

Lint相关的属性

tools:ignore
tools:targetApi
tools:locale

tools:ignore

ignore属性是告诉Lint忽略xml中的某些警告。

假设我们有这样的一个ImageView

<ImageViewandroid: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来忽略这个警告:

<ImageViewandroid: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"android:color="@color/accent_color" />

则Lint会提示警告。

为了不显示这个警告,可以:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:color="@color/accent_color"tools:targetApi="LOLLIPOP" />

tools:locale(本地语言)属性

默认情况下res/values/strings.xml中的字符串会执行拼写检查,如果不是英语,会提示拼写错误,通过以下代码来告诉studio本地语言不是英语,就不会有提示了。

<resourcesxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"tools:locale="it">
<!-- Your strings go here -->
</resources>

这篇文章首先介绍了tools的最基本用法-覆盖android的属性,然后介绍了忽略Lint提示的属性。下篇文章中,我们将继续介绍关于UI预览的其他属性(非android标准属性)。

ps:关于忽略Lint的属性,如果不想了解的话也没关系,因为并不影响编译,一般我都不会管这些警告。

第二部分

这部分我们将继续介绍关于UI预览的其他属性(非android标准属性)。

  • tools:context

  • tools:menu

  • tools:actionBarNavMode

  • tools:listitem/listheader/listfooter

  • tools:showIn

  • tools:layout

tools:context

context属性其实正是的称呼是activity属性,有了这个属性,ide就知道在预览布局的时候该采用什么样的主题。同时他还可以在android studio的java代码中帮助找到相关的文件(Go to Related files)

该属性的值是activity的完整包名

<LinearLayoutxmlns: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>

tools:menu

告诉IDE 在预览窗口中使用哪个菜单,这个菜单将显示在layout的根节点上(actionbar的位置)。

其实预览窗口非常智能,如果布局和一个activity关联(指上面所讲的用tools:context关联)它将会自动查询相关activity的onCreateOptionsMenu方法中的代码,以显示菜单。而menu属性则可以覆盖这种默认的行为。

你还可以为menu属性定义多个菜单资源,不同的菜单资源之间用逗号隔开。tools:menu="menu_main,menu_edit"

如果你不希望在预览图中显示菜单则:tools:menu=""

最后需要注意,当主题为Theme.AppCompat时,这个属性不起作用。

tools:actionBarNavMode

这个属性告诉ide  app bar(Material中对actionbar的称呼)的显示模式,其值可以是

standard
tabs
list
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"tools:actionBarNavMode="tabs" />

同样的,当主题是Theme.AppCompat (r21+, at least) 或者Theme.Material,或者使用了布局包含Toolbar的方式。  该属性也不起作用,只有holo主题才有效。

listitem, listheader 和listfooter 属性

顾名思义就是在ListView ExpandableListView等的预览效果中添加头部 尾部 以及子item的预览布局。

<GridViewandroid: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

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

关于tools 就介绍完了。

注:原文是两篇文章

Tools of the trade — Part 1

Tools of the trade — Part 2 。

android中tools的含义及用法相关推荐

  1. Java中static的含义和用法

    Java中static的含义和用法 static:静态的,用于修饰成员(成员变量,成员方法); 1.被static所修饰的变量或者方法会储存在数据共享区; 2.被static修饰后的成员变量只有一份! ...

  2. python的含义和用法_python中列表的含义及用法

    示例 列表是元素的集合,存储在一个变量中.列表中存储的元素类型没有限制,下面是列表的一个简单例子. students = ['bernice', 'arron', 'cody'] for studen ...

  3. android tools ignore,android 中tools:ignore=UselessParent这个属性的含义是什么?

    满意答案 zjpx456 2018.03.05 采纳率:53%    等级:12 已帮助:7021人 这个属性是给lint检查工具看的,这个告诉IDE 以避免显示这样一条消息: "此 Rel ...

  4. android折叠布局,Android中FoldingLayout折叠布局的用法及实战全攻略

    一.概述无意中翻到的FoldingLayout的介绍的博客,以及github地址.感觉很nice呀,于是花了点时间研究以及编写,本篇博客将带大家从最基本的原理分析,一步一步的实现我们的FoldingL ...

  5. Android中Parcelable与Serializable接口用法

    转自: Android中Parcelable接口用法 1. Parcelable接口 Interface for classes whose instances can be written to a ...

  6. Android中的Handler的具体用法

    Android UI操作并不是线程安全的并且这些操作必须在UI线程中执行.Android利用Handler来实现UI线程的更新的. Handler是Android中的消息发送器,其在哪个Activit ...

  7. Android 中的Intent的某些用法

    在 Android 上使用 Intent 打开视频链接的问题 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType ...

  8. Android中ExpandableListView控件的用法详解

    <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widge ...

  9. Android中wtf的含义

    看Android framework源码时,老是看到诸如"wtf" "reportwtf" 的句子,其实Android中的"wtf"代表了报 ...

最新文章

  1. excel执行INSERT和UPDATE操作语句
  2. CloudStack学习-1
  3. Delphi开发的数据库程序在C:\PDOXUSRS.NET生成文件,拒绝访问及读写权限
  4. cuda的global memory介绍
  5. kernel开启启动log_MySql启动数据库设置初始密码
  6. 目的路径太长如哈删除_win7系统删除文件夹提示“无法访问此文件夹 路径太长”如何解决...
  7. 安卓网络测试工具_【网速】最佳手机网速测试工具
  8. 数学建模算法之动态规划
  9. 算法导论第三版 第3章习题答案
  10. 开源知识管理系统_开源MrDoc,一个适合个人和小型团队的文档、知识、笔记在线管理系统...
  11. 如何将微信聊天记录保存为excel
  12. stm32软件模拟i2c通讯读取lm75a温度
  13. 学习AngularJs:Directive指令用法
  14. 微软C++团队将出席ACCU 2021
  15. 武汉市征集人工智能领域技术成果等通知-2022年申请时间及条件
  16. hostapd wpa_supplicant madwifi详细分析(七)——hostapd整体梳理
  17. 即将上演的5G测量仪器大战
  18. 随机过程:统计独立、正交、不相关 辨析
  19. 计算机基础16秋在线作业,16秋华师《计算机基础》在线作业
  20. 基于EasyNVR二次开发实现自己的摄像机IPC/NVR无插件化直播解决方案

热门文章

  1. 删除字符串中指定的字符形成字符串
  2. java的网络功能与编程_Java的网络功能与编程(转载)
  3. python中的split函数的用法实例_python中的split()函数的用法
  4. 4个mos管驱动的全桥电路原理_逆变电源中的三种保护电路讲解
  5. 数据库要不要放在docker
  6. Golang slice 的底层实现
  7. C# 中使用log4net 日志记录
  8. 推荐几个2021年在Redis、Go和Rust领域异军突起的技术公众号
  9. 假期把技术书籍和焦虑放一边,我做了几天...
  10. java volatile 原子性_为什么volatile不能保证原子性而Atomic可以?