来源:http://tools.android.com/tech-docs/tools-attributes
xmlns:tools让android开发工具可以在xml文件中记录信息,同时这些信息不会被编译进安装文件中。其主要作用是预览和忽略一些检查。
命名空间的uri是xmlns:tools="http://schemas.android.com/tools"
,属性都是以tools
开头。
tools:ignore
忽略属性检测。
这个属性可以在所有的xml中使用
<string name="show_all_apps"
tools:ignore="MissingTranslation">All</string>
tools:targetApi
忽略版本预警。
根java类里面使用@TargetApi
一个作用, 可以用版本名也可以用版本id
<GridLayout tools:targetApi="ICE_CREAM_SANDWICH" >
GridLayout 在api14中引入的,如果程序的minsdk设置小于14,则会预警。
tools:locale
忽略字母拼写检测。
一般在资源文件的根标签中使用,可以设置语言或者时区,比如在values/strings.xml
中设置
<resources xmlns:
tools="http://schemas.android.com/tools"
tools:locale="es">
这样系统就知道,当前默认使用西班牙语而不是英语
ps:只在studio中可用
tools:context
关联activity,布局文件在预览时使用该activity的主题。
一般在布局文件的根标签中使用,
<android.support.v7.widget.GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" ... >
tools:layout
对fragment绑定布局
专门针对fragment的属性,给fragment指定一个布局进行预览
<fragment
android:name="com.example.master.ItemListFragment"
tools:layout="@android:layout/list_content" />
tools:listitem / listheader / listfooter
对AdapterView的子view在预览中设置item,header,footer。
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@android:layout/simple_list_item_2"
tools:listheader="@layout/list_header"
tools:listfooter="@layout/list_footer" />
ps:没有实验成功,不知道为啥。
tools:showIn
如果一个布局被其他布局使用<include>
标签包含进去,可以在当前布局设置这个属性,这样在预览中就会显示整体的效果。
<?xml version="1.0" encoding="utf-8"?>
<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" />
activity_main里面include了这个布局文件。
tools:menu
设置菜单。
这个属性用在一个布局文件的根标签中,在预览中显示actionBar的菜单内容。如果设置了tools:context
属性,则studio将会通过绑定的activity的onCreateOptionsMenu()来获取菜单内容。菜单内容以逗号分隔,同时可以使用menu的资源文件。
<?xml version="1.0" encoding="utf-8"?>
<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:actionBarNavMode
设置actionBar的导航模式。
这个属性用在布局的根标签中。有三种模式: “standard”, “list” and “tabs
<?xml version="1.0" encoding="utf-8"?>
<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" />
PS:只能在studio中使用
tools:shrinkMode,tools:keep,tools:discard
这三个属性是资源保留移除相关的,详细介绍参见