深入理解Android中的xmlns:tools属性

发布时间 - 2026-01-10 21:49:10    点击率:

前言

安卓开发中,在写布局代码的时候,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="I am a title" />

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

用tools吧,别做傻事

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

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

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="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

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

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本地语言不是英语,就不会有提示了。

<resources

xmlns: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标准属性)。

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

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

  1. tools:context
  2. tools:menu
  3. tools:actionBarNavMode
  4. tools:listitem/listheader/listfooter
  5. tools:showIn
  6. tools:layout

tools:context

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

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

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

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的预览布局。

<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

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。


# android  # xmlns  # tools  # xmlns属性  # tools的作用  # Android xmlns 的作用及其自定义实例详解  # 英语  # 只在  # 的是  # 都是  # 使用了  # 也不  # 是在  # 会有  # 带着  # 多个  # 将会  # 也没  # 我都  # 两种  # 他还  # 您可以  # 这部  # 就把  # 这样做  # 就连 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Laravel如何创建和注册中间件_Laravel中间件编写与应用流程  Laravel如何实现URL美化Slug功能_Laravel使用eloquent-sluggable生成别名【方法】  北京企业网站设计制作公司,北京铁路集团官方网站?  html5audio标签播放结束怎么触发事件_onended回调方法【教程】  C#如何调用原生C++ COM对象详解  香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化  如何在IIS中配置站点IP、端口及主机头?  php嵌入式断网后怎么恢复_php检测网络重连并恢复硬件控制【操作】  微信公众帐号开发教程之图文消息全攻略  微博html5版本怎么弄发超话_超话进入入口及发帖格式要求【教程】  如何使用 jQuery 正确渲染 Instagram 风格的标签列表  Laravel路由怎么定义_Laravel核心路由系统完全入门指南  Laravel如何实现API版本控制_Laravel API版本化路由设计策略  JavaScript如何实现路由_前端路由原理是什么  如何实现建站之星域名转发设置?  企业网站制作这些问题要关注  Java Adapter 适配器模式(类适配器,对象适配器)优缺点对比  消息称 OpenAI 正研发的神秘硬件设备或为智能笔,富士康代工  实例解析Array和String方法  企业在线网站设计制作流程,想建设一个属于自己的企业网站,该如何去做?  Java遍历集合的三种方式  大连 网站制作,大连天途有线官网?  微信h5制作网站有哪些,免费微信H5页面制作工具?  Laravel怎么集成Log日志记录_Laravel单文件与每日日志配置及自定义通道【详解】  如何基于云服务器快速搭建网站及云盘系统?  详解Nginx + Tomcat 反向代理 负载均衡 集群 部署指南  Laravel的Blade指令怎么自定义_创建你自己的Laravel Blade Directives  Laravel如何使用Blade模板引擎?(完整语法和示例)  ChatGPT回答中断怎么办 引导AI继续输出完整内容的方法  Laravel如何使用集合(Collections)进行数据处理_Laravel Collection常用方法与技巧  微信小程序 require机制详解及实例代码  ChatGPT怎么生成Excel公式_ChatGPT公式生成方法【指南】  Laravel如何使用Spatie Media Library_Laravel图片上传管理与缩略图生成【步骤】  简单实现jsp分页  如何快速辨别茅台真假?关键步骤解析  Laravel怎么使用Session存储数据_Laravel会话管理与自定义驱动配置【详解】  如何在IIS7中新建站点?详细步骤解析  Laravel怎么实现搜索功能_Laravel使用Eloquent实现模糊查询与多条件搜索【实例】  php静态变量怎么调试_php静态变量作用域调试技巧【解答】  JavaScript如何实现音频处理_Web Audio API如何工作?  长沙企业网站制作哪家好,长沙水业集团官方网站?  如何确认建站备案号应放置的具体位置?  Laravel N+1查询问题如何解决_Eloquent预加载(Eager Loading)优化数据库查询  Laravel用户认证怎么做_Laravel Breeze脚手架快速实现登录注册功能  Laravel控制器是什么_Laravel MVC架构中Controller的作用与实践  微信小程序 scroll-view组件实现列表页实例代码  C++时间戳转换成日期时间的步骤和示例代码  网站制作大概多少钱一个,做一个平台网站大概多少钱?  黑客如何利用漏洞与弱口令入侵网站服务器?  Windows10怎样连接蓝牙设备_Windows10蓝牙连接步骤【教程】