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

本文翻译自:What's “tools:context” in Android layout files?

Starting with a recent new version of ADT, I've noticed this new attribute on the layout XML files, for example: 从最新的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" />

What is "tools:context" used for? 什么是“工具:上下文”用于?

How does it even know the exact path to the activity that is written there? 它怎么知道写在那里的活动的确切路径? Does it look at the package of the app, inside the manifest? 它是否在清单中查看应用程序的包?

Is it limited to classes that extend Context or only activities? 它仅限于扩展Context或仅扩展活动的类吗? Is it usable for ListView items etc.? 它可用于ListView项目等吗?


#1楼

参考:https://stackoom.com/question/kU1H/Android布局文件中的-工具-上下文-是什么


#2楼

This is the activity the tools UI editor uses to render your layout preview. 这是工具UI编辑器用于呈现布局预览的活动。 It is documented here : 它记录在这里

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 这使得编辑器或布局预览中的功能需要活动知识,例如布局主题在预览中应该是什么以及当您从quickfix中进行插入时插入onClick处理程序的位置


#3楼

That attribute is basically the persistence for the "Associated Activity" selection above the layout. 该属性基本上是布局上方“关联活动”选择的持久性。 At runtime, a layout is always associated with an activity. 在运行时,布局始终与活动相关联。 It can of course be associated with more than one, but at least one. 它当然可以与多个,但至少一个相关联。 In the tool, we need to know about this mapping (which at runtime happens in the other direction; an activity can call setContentView(layout) to display a layout) in order to drive certain features. 在该工具中,我们需要知道这个映射(在运行时发生在另一个方向;一个活动可以调用setContentView(布局)来显示布局)以驱动某些功能。

Right now, we're using it for one thing only: Picking the right theme to show for a layout (since the manifest file can register themes to use for an activity , and once we know the activity associated with the layout, we can pick the right theme to show for the layout). 现在,我们只将它用于一件事:选择正确的主题来显示布局(因为清单文件可以注册用于活动的主题,一旦我们知道与布局相关的活动,我们就可以选择正确的主题显示布局)。 In the future, we'll use this to drive additional features - such as rendering the action bar (which is associated with the activity), a place to add onClick handlers, etc. 将来,我们将使用它来驱动其他功能 - 例如渲染操作栏(与活动相关联),添加onClick处理程序的位置等。

The reason this is a tools: namespace attribute is that this is only a designtime mapping for use by the tool. 这是一个工具:namespace属性的原因是这只是工具使用的设计时映射。 The layout itself can be used by multiple activities/fragments etc. We just want to give you a way to pick a designtime binding such that we can for example show the right theme; 布局本身可以被多个活动/片段等使用。我们只想给你一种方法来选择一个设计时绑定,以便我们可以显示正确的主题; you can change it at any time, just like you can change our listview and fragment bindings, etc. 您可以随时更改它,就像您可以更改我们的列表视图和片段绑定等。

(Here's the full changeset which has more details on this ) (这里是这对更详细信息的完整变更

And yeah, the link Nikolay listed above shows how the new configuration chooser looks and works 是的,上面列出 Nikolay 链接显示了新配置选择器的外观和工作方式

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. android打包工具知道忽略它,因此这些属性都不会被打包到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. 它也是存储抑制lint警告的属性的地方 - 作为工具:忽略。


#4楼

According to the Android Tools Project Site : 根据Android工具项目网站

tools:context 工具:上下文

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). 此属性通常在布局XML文件的根元素上设置,并记录布局与哪个活动相关联(在设计时,因为显然布局可以由多个布局使用)。 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. 例如,布局编辑器将使用它来猜测默认主题,因为主题在Manifest中定义并且与活动相关联,而不是布局。 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">  

Used by: Layout editors in Studio & Eclipse, Lint 使用者:Studio和Eclipse中的布局编辑器,Lint


#5楼

“tools:context” is one of the Design Attributes that can facilitate layout creation in XML in the development framework. “tools:context”是可以在开发框架中促进XML格式创建的设计属性之一。 This attribute is used to show the development framework what activity class is picked for implementing the layout. 此属性用于向开发框架显示为实现布局而选择的活动类。 Using “tools:context”, Android Studio chooses the necessary theme for the preview automatically. 使用“工具:上下文”,Android Studio会自动为预览选择必要的主题。

If you'd like to know more about some other attributes and useful tools for Android app development, take a look at this review: http://cases.azoft.com/4-must-know-tools-for-effective-android-development/ 如果您想了解有关Android应用开发的其他一些属性和有用工具的更多信息,请查看以下评论​​: http//cases.azoft.com/4-must-know-tools-for-effective-android -发展/


#6楼

tools:context=".MainActivity" thisline is used in xml file which indicate that which java source file is used to access this xml file. tools:context=".MainActivity" thisline在xml文件中使用,表示使用哪个java源文件来访问此xml文件。 it means show this xml preview for perticular java files. 它意味着显示特定java文件的xml预览。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值