关于xml:Android布局文件中的“工具:上下文”是什么?

What's “tools:context” in Android layout files?

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

1
2
3
4
5
6
<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" />

"工具:上下文"用于什么?

它怎么知道写在那里的活动的确切路径?它是否在清单中查看应用程序的包?

它仅限于扩展上下文的类还是仅限于活动?它是否可用于ListView项等?


这是工具UI编辑器用于呈现布局预览的活动。记录如下:

This attribute declares which activity this layout is associated with by default. This enables features in the editor or layout preview that require knowledge of the activity, such as what the layout theme should be in the preview and where to insert onClick handlers when you make those from a quickfix


该属性基本上是布局上方"关联活动"选择的持久性。在运行时,布局始终与活动关联。当然,它可以与多个关联,但至少可以与一个关联。在这个工具中,我们需要了解这个映射(在运行时发生在另一个方向;活动可以调用setContentView(layout)来显示一个布局),以便驱动某些特性。

现在,我们只将其用于一件事:为布局选择要显示的正确主题(因为清单文件可以为活动注册要使用的主题,并且一旦知道与布局关联的活动,我们就可以为布局选择要显示的正确主题)。在将来,我们将使用它来驱动其他功能,例如呈现操作栏(与活动关联)、添加onclick处理程序的位置等。

这是一个tools:namespace属性的原因是,这只是一个供工具使用的设计时映射。布局本身可以由多个活动/片段等使用。我们只想给您一种选择设计时绑定的方法,例如,我们可以显示正确的主题;您可以随时更改它,就像您可以更改ListView和片段绑定等。

(以下是完整的变更集,其中有更多详细信息)

是的,上面列出的链接nikolay显示了新配置选择器的外观和工作方式

还有一件事:"工具"名称空间是特殊的。Android打包工具知道忽略它,所以这些属性都不会打包到APK中。我们将它用于布局中的额外元数据。例如,在这里还存储了抑制lint警告的属性——作为工具:忽略。


根据Android工具项目网站:

工具:语境

此属性通常设置在布局XML文件中的根元素上,并记录与布局关联的活动(在设计时,因为显然一个布局可以由多个布局使用)。例如,布局编辑器将使用它来猜测默认主题,因为主题在清单中定义并且与活动(而不是布局)关联。您可以使用与清单中相同的点前缀,只指定不带完整应用程序包名称作为前缀的活动类。

1
2
3
4
<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


1描述。

它不会tools: context ="activity name"包装到apk。只有ADT布局编辑器的布局文件渲染上下文设置相应的电流,电流显示的布局是在渲染上下文名称对应的作业的作业活动,如果在manifest文件集一个主题,然后想ADT布局编辑器渲染流布局根据你的主题。如果你MainActivity设置意味着设置一个主题。光(其他的),然后你看到的布局在视觉背景经理应控制什么主题。光的样子。只告诉你"你看到的就是你得到的结果。

一些人想知道一些湖湖,所以有些人不知道,我会添加一些解释的话:

样品2。

Take a simple tools:text, for example, some more image, convenient to further understand the tools:context

1
2
3
4
5
6
7
8
9
10
11
<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" />

enter image description here

TextView1 adopted the android: text, and use the tools:text in the TextView2, on the right side of the Layout editor will display the sample name1, the sample name2 two font, if after you run the code to compile, generated apk, terminal display only the sample name1, does not show the sample name2 the words. You can try to run, see how the effect.

3特异性描述。

1,tools: context ="activity name"它不会进入包装apk(等效的理解:这是该种效应,NO)。

2。只有ADT布局编辑器(IU)上面的图标,在右侧的模拟器)中相应的文件集的渲染上下文流布局,布局的XML流的上下文中绘制的名字对应的作业的作业活动,如果在清单文件集一个主题,然后ADT布局编辑器你想渲染流布局按照主题。如果你MainActivity设置意味着设置一个主题。光可以被(其他)。(理解:你是tools: context ="activity name"添加XML布局,绘制指定的作业,在《宣言establishes主题文件,主题风格pictured模拟器上面对后续的变化,因此相应的主题。)

4.summary

论文的主要工作是和上面的属性,在一aimed工具,模拟器调试和编译时间显示状态,不工作


"tools:context"是一个设计属性,可以帮助在开发框架中以XML创建布局。此属性用于向开发框架显示为实现布局而选择的活动类。使用"工具:上下文",android studio自动选择预览所需的主题。

如果您想了解更多有关Android应用程序开发的其他属性和有用工具的信息,请查看以下评论:http://cases.azoft.com/4-must-know-tools-for-effective-android-development/


解决方案:这是最好的http://developer.android.com /工作室/写/工具属性

这是一个设计属性我们可以设置在XML上下文样活性

1
tools:context=".activity.ActivityName"

适配器:

1
tools:context="com.PackegaName.AdapterName"

enter image description here

你可以浏览到Java类的标志,当点击图标有更多的功能和工具类

1
2
3
tools:text=""
tools:visibility:""
tools:listItems=""//for recycler view

ETX


tools:context=".MainActivity"这一行用于XML文件,它指示使用哪个Java源文件来访问该XML文件。它意味着为Java Java文件显示XML预览。