有些效果必须在运行之后才能看见,比如TextView在xml中没有设置任何字符,在activity中设置了text,因此为了在AS中预览效果,你可能会在xml中设置android:text属性来预览效果:
<TextView android:id="@+id/text_main" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello"/>一般这样做之后在最终发布之前你仍然需要将android:text="Hello"这行代码删除,但是存在的问题就是你很可能会忘记,而且如果代码中存在很多这样的代码,删除也是个很大的工作量,这时候你应该想到tools,我们只需要用如下的方法即可解决:
xmlns:tools="http://schemas.android.com/tools"tools可以告诉AS,哪些属性在运行的时候是被忽略的,只在设计的时候可以看到,在运行的时候tools本身也是被忽略的,不会被带进APK中。
tools属性可以分为两类,一种是影响Lint提示的,一种是关于xml布局设计的。
lint相关的属性主要有以下三个:
tools:ignore tools:targetApi tools:localetools:ignore
ignore属性时告诉lint忽略xml中的某些警告,假设我们有个ImageView如下:
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" 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: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中的字符串会执行拼写检查,如果不是英语,会提示拼写错误,通过以下代码来告诉AS本地语言不是英语:
<resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:locale="it"> </resources>xml布局相关的属性主要有以下几个:
tools:contexttools:menutools:actionBarNavModetools:listitem/listheader/listfootertools:showIntools:layout该属性的值通常是activity的完整包名,例如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.adnroid.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:lauouy_height="match_parent" android:orientation="vertical" tools:context="com.android.example.MainActivity"> </LinearLayout>告诉AS在预览窗口中使用哪个菜单,这个菜单将显示在layout的根结点上(actionbar的位置)。
其实预览窗口非常智能,如果布局和一个activity关联(tools:context进行关联),它将会自动查询相关activity的onCreateOptionsMenu方法中的代码,以显示菜单,而menu属性可以覆盖这种行为。
还以可以在menu属性中定义多个菜单资源,不同的菜单资源之间用逗号隔开。
tools:menu="menu_main,menu_edit"如果不希望在预览图中显示菜单则:
tools:menu=""需要注意的是:当主题为Theme.AppCompat时,这个属性不起作用
这个属性告诉AS,app bar的显示模式,其值可以是:
standardtabslist同样的,当主题为Theme.AppCompat或者Theme.Material该属性不起作用,只有holo主题才有效。
指在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" />tools:layout告诉AS,fragment在程序预览的时候该显示什么样:
<fragment xmlns="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" />该属性设置于一个被其他布局<include>的布局的根元素上,这样可以指向包含此布局的其中一个布局,在设计时这个被包含的布局会带着周围的外部布局被渲染。
本文摘录自speedboy007的博客,需要了解更多的可以去详细阅读!