一开始不明白,后来删掉这个属性之后发现会出现一个提示:

pick preview layout from the "Fragment Layout" context menu

原来tools:layout仅仅是告诉编辑器,Fragment在程序预览的时候该显示成什么样,并不会对apk产生实际作用,是为开发者设计的。

一般来说被xmlns:tools="http://schemas.android.com/tools" 声明的tools作为前缀的属性都不会被编译进去。这个跟上面代码中tools:context是一样的。

http://blog.csdn.net/zhengdan66/article/details/46960985

Android开发中在布局文件里面都会有如下面的内容:

<LinearLayoutxmlns: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:context=".MainActivity"

/>

其中tools起什么作用呢?可以参考文档http://tools.android.com/tech-docs/tools-attributes

在该文档中列举了很多tools的方法,比如tools:ignore/tools:targetApi/tools:locale等方法,但是我们今天要讨论的则是该文档中最后面提到的内容Designtime Attributes,文档地址为http://tools.android.com/tips/layout-designtime-attributes

在官方文档中有说这么一句: These are attributes which are used when the layout is rendered inthe tool, but have no impact on the runtime. This is useful if you for examplewant to put sample data in your textfields for when you are editing the layout,but you don't want those attributes to affect your running app.

这些属性用于渲染布局,而不会影响到程序运行。也就是说只在预览布局时出现,在程序运行时相当于该属性不存在。

这个说明非常有意思,比如我们在布局文件中想要显示一段文字时,而该文字内容在程序中可能动态变化,特别是有参数的字符串内容%1$s之类的内容,以前必须使用android:text显示,然后调整这段文字的大小颜色参数,然后完成后删除android:text属性。有了tools参数后,可以直接使用tools:text在预览时显示文字即可,省却了上面删除的麻烦。

目前常用的有tools:text, tools:visibility, tools.src, tools.background

官方文档中也列出了一些限制,如下:

  • Currently only overriding existing attributes is supported. We may want to define some additional convenience attributes to make it simple to for example choose which child in a ViewFlipper to show etc. 当前只支持overriding已有的属性
  • You have to manually edit in your designtime attributes at this time
    • They do not appear as an option in for example the layout editor property sheet.
    • Editor code completion does not help you enter these attributes; the easiest way to use them is to first enter them in the Android namespace, and when done replacing the prefix. 目前还只能手动输入该部分,代码自动填充还无法完成
  • Note that designtime attributes are supported only for layout files themselves. You cannot use them anywhere else -- in menu XML files, in string resource definitions, etc.只支持layout布局,menu/resource/string暂时不支持,这个说法和http://tools.android.com/tech-docs/tools-attributes中内容有冲突,tools-attributes中提供了<string name="show_all_apps" tools:ignore="MissingTranslation">All</string>以及<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="es">内容
  • Designtime attributes can only be used for framework resources, not custom attributes at this point.只能适用于系统提供的属性,自定义属性不再这个范围
  • See https://code.google.com/p/android/issues/detail?id=46186 for background or additional requests or comments.

android xmlns:tools用法的更多相关文章

  1. android中xmlns:tools属性详解

    今天读到一篇总结的非常棒的文章,写的逻辑很清晰也很实用,很少见到如此棒的文章了.就原文转发过来,我把格式给整理了一下,分享给园子里的各位朋友!好久没写博客了,就为2015年的11月留份纪念吧.希望对你 ...

  2. 【Android学习】android:layout_weight的用法实例

    对于android:layout_weight的用法,用下面的例子来说明: <LinearLayout xmlns:android="http://schemas.android.co ...

  3. 2、android Service 详细用法

    定义一个服务 在项目中定义一个服务,新建一个ServiceTest项目,然后在这个项目中新增一个名为MyService的类,并让它继承自Service,完成后的代码如下所示: ? 1 2 3 4 5 ...

  4. android fragment 的用法以及与activity的交互和保存数据的方法,包括屏幕切换(转载)!

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37992017 1.管理Fragment回退栈 类似与Android系统为Acti ...

  5. Android layout_weight的用法

    android:layout_weight是指LinearLayout先给里面的控件分配完大小之后剩余空间的权重. 下面通过举例说明: <LinearLayout xmlns:android=& ...

  6. Android 之 tools:context和tools:ignore两个属性的作用

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  7. 比较强大 优秀的开源框架 :Android图片加载与缓存:Android Glide 的用法

    使用Android Glide,需要先下载Android Glide的库,Android Glide在github上的项目主页: https://github.com/bumptech/glide . ...

  8. xmlns:tools="http://schemas.android.com/tools"以及tools:context=".ConfActivity"是什么意思

    xmlns:tools="http://schemas.android.com/tools"这个是xml的命名空间,有了他,你就可以alt+/作为提示,提示你输入什么,不该输入什么 ...

  9. Android之Adapter用法总结-(转)

    Android之Adapter用法总结 1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(List View,Grid Vie ...

随机推荐

  1. window.history.pushState与ajax实现无刷新更新页面url

    ajax能无刷新更新数据,但是不能更新url HTML5的新API: window.history.pushState, window.history.replaceState 用户操作history ...

  2. 将数据导出到Excel2007格式。

    增加数据格式 public static void TableToExcel2(DataTable table, string filename, string sheetname) { XSSFWo ...

  3. 小白学数据分析----->ARPPU的误区

    新年到来,该应该持续坚持写下去,还是有很多人要来学习和进步的. 今天提到了一个概念:ARPPU. 这个概念等同于之前大家认识的ARPU(其实这句话我是很不愿意说的),ARPPU是总收入除以总付费用户数 ...

  4. ios相关手册、图表等综合

    Objective-C初学者速查表(来源:http://www.cocoachina.com/applenews/devnews/2013/1115/7362.html) iOS UIKit类图 (来 ...

  5. 解决Visual Studio 2010/2012在调试时lock文件的方法

    调试3dsmax插件,有一个避免每次修改插件代码都需要重启3dsmax的方法,就是将导出的核心代码写在一个独立的DLL中,然后在插件代码需要导出时LoadLibrary这个DLL,导出之后再FreeL ...

  6. Oracle Redo Log 机制 小结(转载)

    Oracle 的Redo 机制DB的一个重要机制,理解这个机制对DBA来说也是非常重要,之前的Blog里也林林散散的写了一些,前些日子看老白日记里也有说明,所以结合老白日记里的内容,对oracle 的 ...

  7. libevent 安装异常

    有homebrew的可以使用 1 brew install memcached 这个命令来安装没有homebrew的可以直接手动安装1.去官网http://memcached.org/下载最新的包,然 ...

  8. [Aaronyang] 写给自己的WPF4.5 笔记22 [3d交互与动画 3/4]

    OK,前面我们的3d模型都比较囧啊,最近也看了一点ZAM了解了一下,大致至少可以做个简单的模型用来演示. 1.交互,动起来的思路 ①修改Model3D对象的变换 ②修改应用于ModelVisual3D ...

  9. Enclosure POJ

    0:Enclosure http://poj.openjudge.cn/challenge3/0/ 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  131072kB 描述 为了防止 ...

  10. Web service standards: SOAP, REST, OData, and more

    Web service standards: SOAP, REST, OData, and more So far, we've covered the components of a web ser ...