从最近ADT的新版本开始,我注意到布局XML文件上的这个新属性,例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" />

“工具:上下文”的用途是什么?

它怎么知道写在那里的活动的确切路径?它会查看manifest里面的应用程序包吗?

它局限于扩展Context的类还是仅仅局限于活动?它对ListView项目等有用吗?


当前回答

此属性有助于获得与布局相关的活动的最佳知识。当您必须使用QuickFix在视图上添加onClick处理程序时,这也很有用。

tools:context=".MainActivity"

其他回答

根据Android工具项目网站:

工具:上下文

This attribute is typically set on the root element in a layout XML file, and records which activity the layout is associated with (at designtime, since obviously a layout can be used by more than one layout). This will for example be used by the layout editor to guess a default theme, since themes are defined in the Manifest and are associated with activities, not layouts. You can use the same dot prefix as in manifests to just specify the activity class without the full application package name as a prefix.

<android.support.v7.widget.GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"    
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">  

用于:Studio和Eclipse中的布局编辑器,Lint

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    //more views

</androidx.constraintlayout.widget.ConstraintLayout>

在上面的代码中,工具:context的基本需求是默认情况下告诉布局文件与哪个活动或片段相关联。因此,您可以使用与Manifest文件中使用的相同的点前缀指定活动类名。

通过这样做,Android Studio将自动为预览选择必要的主题,你不必手动进行预览设置。我们都知道一个布局文件可以与几个活动相关联,但是主题是在Manifest文件中定义的,这些主题与您的活动相关联。因此,通过在布局文件中添加tools:context, Android Studio预览将自动为你选择必要的主题。

此属性声明默认情况下该布局与哪个活动相关联。这使编辑器或布局预览中需要活动知识的功能成为可能。

此属性有助于获得与布局相关的活动的最佳知识。当您必须使用QuickFix在视图上添加onClick处理程序时,这也很有用。

tools:context=".MainActivity"

1.描述

tools: context = "activity name" it won't be packaged into the apk .Only ADT Layout Editor in your current Layout file set corresponding rendering context, show your current Layout in rendering the context is the activity name corresponds to the activity, if the activity in the manifest file set a Theme, then ADT Layout Editor will render your current Layout according to the Theme.Means that if you set the MainActivity set a Theme. The Light (the other), then you see in visual layout manager o background control of what should be the Theme. The Light looks like.Only to show you what you see is what you get results.

有的人看了会懂一些,有的人看了也不知道,我再加几句解释:

2.样本

以简单的工具:文字为例,多一些图像,方便进一步了解的工具:上下文

<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="sample name1" />

<TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:text="sample name2" />

TextView1采用了android: text,并使用工具:text在TextView2中,在布局编辑器的右侧会显示样本name1、样本name2两种字体,如果在运行代码编译后,生成apk,终端只显示样本name1,不显示样本name2的文字。你可以试试跑,看看效果如何。

3.具体的描述

1.这些工具:context = "activity name"它不会被打包到apk中(理解:相当于这个是注释的,编译后没有效果。)

2.Only ADT Layout Editor (i.e., for the above icon on the right side of the simulator) in the current Layout file set corresponding rendering context, the Layout of the current XML in rendering the context is the activity name corresponds to the activity, if the activity in the manifest file set a Theme, then ADT Layout Editor will render your current Layout according to the Theme.Means that if you set the MainActivity set a Theme. The Light can also be (other).(understand: you added tools: context = "activity name", the XML layout is rendering specified activity, establishes a Theme in the manifest file, pictured above right simulator Theme style will also follow changes corresponding to the Theme.)

4.总结

综上所述,以上这些属性主要针对正确的工具,模拟器调试时间显示状态,以及编译不工作,