سؤال

There is a pulldown menu next to the theme pulldown; its hover text reads "associated activity or fragment providing context".

This (new) widget is not described (or shown) in the graphical layout editor documentation.

I assume that this is an activity or fragment that typically displays this view. But what does the layout editor do with this information? Can it somehow be used to preview action bar customizations?

enter image description here

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top