3

主题下拉菜单旁边有一个下拉菜单;它的悬停文本显示为“提供上下文的相关活动或片段”。

此(新)小部件未在图形布局编辑器文档中描述(或显示) 。

我假设这是一个通常显示此视图的活动或片段。但是布局编辑器如何处理这些信息?它可以以某种方式用于预览操作栏自定义吗?

在此处输入图像描述

4

1 回答 1

6

The Android tools blog documented it as follows:

We've added a new menu: The Activity chooser. This lets you choose the activity context to use for this layout:

enter image description here

When you open a layout the first time, we attempt to guess the right layout (by looking at your activity classes and seeing which layouts they reference), but you can choose a different activity here. The activity associated with a layout is used to pick which theme to render with, in the case where you've assigned themes to activities in your manifest file. You can also open the activity in the Java editor by choosing "Open activity name". In the future we will also use the activity<->layout association to drive other features.

I noticed after changing this value, a new tools:context attribute was added to the top-level element in the XML, which led me to this related question and answer. I've also entered a documentation bug report requesting that it be included in the documentation.

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.

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.

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: https://android-review.googlesource.com/#/c/36273/ )

And yeah, the link Nikolay listed above shows how the new configuration chooser looks and works: http://tools.android.com/recent/newconfigchooser

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.

于 2012-09-19T20:14:58.167 回答