android xml布局文件中tools:layout的作用

bisdd62qa 8年前

用最新版本的adt 创建一个基于master/detail flow 模版的app的时候,生成的activity_item_list.xml 文件中有一个tools:layout属性:

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:context=".ItemListActivity"      tools:layout="@android:layout/list_content" />

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

pick preview layout from the "Fragment Layout" context menu

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

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