android tools:context=”.MainActivity”与xmlns:tools名字空间

ADT20.0,新建Android Application Project:

默认的布局文件内容变化了:
LinearLayout变成了RelativeLayout:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
android:textSize="18sp"
tools:context=".MainActivity" />

还多了个新的namespace和相应的属性,这个属性的作用是什么呢?解释如下:

请各自选择相应语种,对号入座,O(∩_∩)O~

English Original Explaination:
引用
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:context="activity name"这一句不会被打包进APK。只是ADT的Layout Editor在你当前的Layout文件里面设置对应的渲染上下文,说明你当前的Layout所在的渲染上下文是activity name对应的那个activity,如果这个activity在manifest文件中设置了Theme,那么ADT的Layout Editor会根据这个Theme来渲染你当前的Layout。就是说如果你设置的MainActivity设置了一个Theme.Light(其他的也可以),那么你在可视化布局管理器里面看到的背景阿控件阿什么的就应该是Theme.Light的样子。仅用于给你看所见即所得的效果而已。

猜你喜欢

转载自iaiai.iteye.com/blog/1935278