Android -- tools

工具属性

Android 有一个专用的XML命名空间,用于使工具可以记录XML文件里的信息,并且在打包程序的进行把信息剥离到不会带来运行时期和下载大小的负面影响的程度。 这个命名空间的 URI 是 http://schemas.android.com/tools,并且它通常被绑定到 tools: 前缀中:

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

这个tools标签主要是为adt插件使用的。他里面的很多属性能在很大程度上方便我们的开发,但是并不会影响我们最终生成的apk包。比如大家在写一个界面的时候一般都会给Textview写上text的值,然后在开发完毕的时候再删除他,这个操作就很麻烦,但是现在你就可以。

tools:ignore

此属性可以在任何 XML 元素上设置,它是一个逗号分割的lint 问题ID的列表,表示了应该要在此元素或它的任何子元素上递归忽略的lint问题的ID。

<string name="show_all_apps" tools:ignore="MissingTranslation">All</string>

tools:context

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
            xmlns:tools="http://schemas.android.com/tools"  
            android:orientation="vertical"  
            android:layout_width="fill_parent"  
            android:layout_height="fill_parent"  
            tools:context=".MainActivity"    
        />  

tools:context=".MainActivity"这一句不会被打包进APK。只是ADT的Layout Editor在你当前的Layout文件里面设置对应的渲染上下文,说明你当前的Layout所在的渲染上下文是activity name对应的那个activity,如果这个activity在manifest文件中设置了Theme,那么ADT的Layout Editor会根据这个Theme来渲染你当前的Layout。仅用于给你看所见即所得的效果而已。(One more thing: The "tools" namespace is special. The android packaging tool knows to ignore it, so none of those attributes will be packaged into the APK. We're using it for extra metadata in the layout. It's also where for example the attributes to suppress lint warnings are stored -- as tools:ignore.)

tools:targetApi

此属性像 Java 类中的 @TargetApi 批注解一样: 它允许您指定一个 API 级别,可以是整数或代码名称,表示此元素需要在此级别之上运行。

<GridLayout tools:targetApi="ICE_CREAM_SANDWICH" 
            .........
            >

tools:text

<TextView 
         android:text="text"
         tools:text="tools text"
         .........
          >

tools:text,其实就是给ADT用的,用于在design页面能够预览到这个属性的值,但是当实际上运行的时候是看不到这个值的。

tools:listitem / listheader / listfooter

是给ADT来让你预览listview布局的。

<ListeView
           tools:listview="@android:layout/simple_list_item_1"
           .........
           >

tools:locale

此属性可以设置在资源文件的根元素上,并且应该对应于一种语言或一个地区。这会让工具知道文件的字符串被假定为哪种语言(区域)中的。例如, values/strings.xml 可以有此根元素:

<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="es">

tools:layout

此属性通常设置在一个 标签中,用来记录在设计时你想看到渲染的布局 (在运行时,将由这个标签所列的fragment的类的操作所决定)。

<fragment 
          android:name=".MyFragment" 		    			tools:layout="@android:layout/list_content" />

tools:showIn

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

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:showIn="@layout/activity_main" />

tools:menu

这个属性在布局的根元素上设置,用于配置在 Action Bar中显示的菜单。Android Studio 通过查看这个布局文件所链接的activity(通过 tools:context)里的onCreateOptionsMenu()方法,尝试找出哪些菜单在 ActionBar 中使用。它允许重写哪个搜索和显示声明的菜单用于显示。它的值是逗号分隔的 id 列表 (没有 @id/ 或任何这类前缀)。还可以使用没有.xml 扩展名的菜单xml文件的名称。

<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:menu="menu1,menu2" />

tools:actionBarNavMode

这个属性在布局的根元素上设置,用于配置 Action Bar 使用的导航模式。可能的值包括:“standard”,“list”和“tabs”。

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

更多

http://tools.android.com/tips/layout-designtime-attributes

我是天王盖地虎的分割线

posted @ 2015-09-07 14:45  我爱物联网  阅读(1244)  评论(0编辑  收藏  举报
AmazingCounters.com