android中xmlns:tools属性详解

第一部分

安卓开发中,在写布局代码的时候,ide能够看到布局的预览效果。java

可是有些效果则必须在运行以后才能看见,好比这种状况:TextView在xml中没有设置任何字符,而是在activity中设置了text。所以为了在ide中预览效果,你必须在xml中为TextView控件设置android:text属性android

 

复制代码
<TextView

android:id="@+id/text_main"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textAppearance="@style/TextAppearance.Title"

android:layout_margin="@dimen/main_margin"

android:text="I am a title" />
复制代码

通常咱们在这样作的时候都告诉本身,不要紧,等写完代码我就把这些东西一并删了。可是你可能会忘,以致于在你的最终产品中也会有这样的代码。api

用tools吧,别作傻事

以上的状况是能够避免的,咱们使用tools命名空间以及其属性来解决这个问题。app

xmlns:tools="http://schemas.android.com/tools"ide

tools能够告诉Android Studio,哪些属性在运行的时候是被忽略的,只在设计布局的时候有效。好比咱们要让android:text属性只在布局预览中有效能够这样布局

复制代码
<TextView

android:id="@+id/text_main"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textAppearance="@style/TextAppearance.Title"

android:layout_margin="@dimen/main_margin"

tools:text="I am a title" />
复制代码

tools能够覆盖android的全部标准属性,将android:换成tools:便可。同时在运行的时候就连tools:自己都是被忽略的,不会被带进apk中。spa

tools属性的种类

tools属性能够分为两种:一种是影响Lint提示的,一种是关于xml布局设计的。以上介绍的是tools的最基本用法:在UI设计的时候覆盖标准的android属性,属于第二种。下面介绍Lint相关的属性。设计

Lint相关的属性3d

tools:ignore

tools:targetApi

tools:locale

tools:ignorecode

ignore属性是告诉Lint忽略xml中的某些警告。

假设咱们有这样的一个ImageView

复制代码
<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="@dimen/margin_main"

android:layout_marginTop="@dimen/margin_main"

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:layout_marginStart="@dimen/margin_main"

android:layout_marginTop="@dimen/margin_main"

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中的字符串会执行拼写检查,若是不是英语,会提示拼写错误,经过如下代码来告诉studio本地语言不是英语,就不会有提示了。

复制代码
<resources

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

xmlns:tools="http://schemas.android.com/tools"

tools:locale="it">


<!-- Your strings go here -->


</resources>
复制代码

这篇文章首先介绍了tools的最基本用法-覆盖android的属性,而后介绍了忽略Lint提示的属性。下篇文章中,咱们将继续介绍关于UI预览的其余属性(非android标准属性)。

ps:关于忽略Lint的属性,若是不想了解的话也不要紧,由于并不影响编译,通常我都不会管这些警告。

第二部分

这部分咱们将继续介绍关于UI预览的其余属性(非android标准属性)。

  • tools:context

  • tools:menu

  • tools:actionBarNavMode

  • tools:listitem/listheader/listfooter

  • tools:showIn

  • tools:layout

tools:context

context属性其实正是的称呼是activity属性,有了这个属性,ide就知道在预览布局的时候该采用什么样的主题。同时他还能够在android studio的java代码中帮助找到相关的文件(Go to Related files

该属性的值是activity的完整包名

复制代码
<LinearLayout

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

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/container"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="com.android.example.MainActivity">  <!-- ... -->

</LinearLayout>
复制代码

 

tools:menu

告诉IDE 在预览窗口中使用哪一个菜单,这个菜单将显示在layout的根节点上(actionbar的位置)。

其实预览窗口很是智能,若是布局和一个activity关联(指上面所讲的用tools:context关联)它将会自动查询相关activity的onCreateOptionsMenu方法中的代码,以显示菜单。而menu属性则能够覆盖这种默认的行为。

你还能够为menu属性定义多个菜单资源,不一样的菜单资源之间用逗号隔开。

 

tools:menu="menu_main,menu_edit"

若是你不但愿在预览图中显示菜单则:

 

tools:menu=""

最后须要注意,当主题为Theme.AppCompat时,这个属性不起做用。

tools:actionBarNavMode

这个属性告诉ide  app bar(Material中对actionbar的称呼)的显示模式,其值能够是

standard

tabs

list

 

复制代码
<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" />
复制代码

 

一样的,当主题是Theme.AppCompat (r21+, at least) 或者Theme.Material,或者使用了布局包含Toolbar的方式。  该属性也不起做用,只有holo主题才有效。

listitem, listheader 和listfooter 属性

顾名思义就是在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" />
复制代码

layout属性

tools:layout告诉ide,Fragment在程序预览的时候该显示成什么样

复制代码
<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:layout="@android:layout/list_content" />
复制代码

 

tools:showIn 该属性设置于一个被其余布局<include>的布局的根元素上。这让您能够指向包含此布局的其中一个布局,在设计时这个被包含的布局会带着周围的外部布局被渲染。这将容许您“在上下文中”查看和编辑这个布局。须要 Studio 0.5.8 或更高版本。