Android 布局文件中的“工具:上下文”是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11078487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's "tools:context" in Android layout files?
提问by android developer
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? Is it usable for ListView items etc.?
它仅限于扩展上下文的类还是仅限于活动?它可用于 ListView 项目等吗?
采纳答案by Nikolay Elenkov
This is the activity the tools UI editor uses to render your layout preview. It is documented here:
这是工具 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
此属性声明此布局默认与哪个活动相关联。这将启用编辑器或布局预览中需要了解活动的功能,例如预览中的布局主题应该是什么,以及当您从快速修复中创建 onClick 处理程序时在哪里插入这些处理程序
回答by Tor Norbye
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(layout) 来显示布局)以驱动某些功能。
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. 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 linkNikolay 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. 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.
还有一件事:“工具”命名空间很特别。android 打包工具知道忽略它,因此这些属性都不会打包到 APK 中。我们将它用于布局中的额外元数据。例如,它也是存储抑制 lint 警告的属性的地方——作为工具:忽略。
回答by anothercoder
According to the Android Tools Project Site:
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). 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.
该属性通常设置在布局 XML 文件中的根元素上,并记录布局与哪个活动相关联(在设计时,因为显然一个布局可以被多个布局使用)。例如,布局编辑器将使用它来猜测默认主题,因为主题是在清单中定义的并且与活动相关联,而不是与布局相关联。您可以使用与清单中相同的点前缀来指定活动类,而无需将完整的应用程序包名称作为前缀。
<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 中的布局编辑器
回答by KeLiuyue
1.Description
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.
tools: context = "activity name"
它不会被打包到apk
.Only ADT
Layout Editor 在你当前的 Layout 文件中设置对应的渲染上下文,在渲染上下文中显示你当前的 Layout 是 Activity 名称对应的 Activity,如果manifest
文件中的 Activity设置了 Theme,那么ADT
Layout Editor 将根据 Theme.Means 呈现您当前的 Layout,如果您设置了MainActivity
一个 Theme。The Light(另一个),然后你会在视觉布局管理器中看到什么应该是 Theme 的背景控制。光看起来像。只为向您展示所见即所得。
Some people see will understand some, some people see the also don't know, I'll add a few words of explanation:
有的人看懂一些,有的人看不懂,我补充几句解释:
2.Sample
2.样品
Take a simple
tools:text
, for example, some more image, convenient to further understand thetools:context
tools:text
举个简单的例子,多一些图,方便进一步理解tools:context
<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" />
TextView
1 adopted theandroid: text
, and use thetools:text
in theTextView
2, on the right side of the Layout editor will display thesample name1
, thesample name2
two font, if after you run the code to compile, generatedapk
, terminal display only thesample name1
, does not show thesample name2
the words. You can try to run, see how the effect.
TextView
1采用的android: text
,并使用tools:text
在TextView
图2,在布局编辑器的右侧将显示sample name1
,所述sample name2
两种字体,如果运行的代码编译后,生成的apk
,终端只显示sample name1
,不显示sample name2
的话。你可以试试运行,看看效果如何。
3.Specific description
3.具体说明
1.The tools: context = "activity name"
it won't be packaged into the apk
(understanding: the equivalent of this is commented, the compiled no effect.)
1.tools: 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.)
2.只有ADT
Layout Editor(即模拟器右侧的上面那个图标)在当前的Layout文件中设置了对应的渲染上下文,当前XML在渲染上下文中的Layout就是activity名称对应的activity,如果manifest文件中的activity设置了一个Theme,那么ADT
Layout Editor会根据Theme渲染你当前的Layout。意思就是如果你设置了MainActivity
一个Theme。Light也可以是(other)。(理解:你加了tools: context = "activity name"
,xml布局是渲染指定的activity,在manifest文件中建立一个Theme,上图右边模拟器的Theme风格也会跟着Theme对应的变化。)
4.summary
4.总结
To sum up, these properties mainly aimed at above the right tools, the simulator debugging time display status, and compile doesn't work,
综上所述,这些属性主要针对上面对工具,模拟器调试时间显示状态,以及编译不起作用,
回答by Mihir Patel
“tools:context” is one of the Design Attributes that can facilitate layout creation in XML in the development framework. 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.
“tools:context”是设计属性之一,可以促进在开发框架中以 XML 格式创建布局。此属性用于向开发框架显示为实现布局而选择的活动类。使用“tools:context”,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 -发展/
回答by Ashik Azeez
This is best solution : https://developer.android.com/studio/write/tool-attributes
这是最好的解决方案:https: //developer.android.com/studio/write/tool-attributes
This is design attributes we can set activty context in xml like
这是我们可以在 xml 中设置活动上下文的设计属性,例如
tools:context=".activity.ActivityName"
Adapter:
适配器:
tools:context="com.PackegaName.AdapterName"
You can navigate to java class when clicking on the marked icon and tools have more features like
单击标记的图标时,您可以导航到 java 类,并且工具具有更多功能,例如
tools:text=""
tools:visibility:""
tools:listItems=""//for recycler view
etx
以太坊
回答by Nikita Prajapati
tools:context=".MainActivity"
thisline is used in xml file which indicate that which java source file is used to access this xml file.
it means show this xml preview for perticular java files.
tools:context=".MainActivity"
thisline 用在 xml 文件中,表示使用哪个 java 源文件来访问这个 xml 文件。这意味着为特定的 java 文件显示此 xml 预览。