Visible
Similarly, items and groups can be visible or invisible, controlled in the XML via the attribute on the item or group element. By default, items and groups are visible. Invisible items and groups do not appear in the menu at all. You can change an item’s status at runtime via the method on , or change a group’s status via on .
In the layout XML shown earlier, the group is initially invisible. If we make it visible in our Java code, the two menu items in the group will “magically” appear.
Shortcut
Items can have shortcuts – single letters () or numbers () that can be pressed to choose the item without having to use the touch screen, D‑pad, or trackball to navigate the full menu.
Inflating a Menu
Actually using a menu, once it’s defined in XML, is easy. Just create a and tell it to inflate your menu:
Public onCreateOptionsMenu
New MenuInflater getApplication
Inflate
Return super onCreateOptionsMenu
CHAPTER 12
Fonts
Inevitably, you’ll get the question “Hey, can we change this font?” when doing application development. The answer depends on what fonts come with the platform, whether you can add other fonts, and how to apply them to the widget or whatever needs the font change.
Android is no different. It comes with some fonts plus a means for adding new fonts. Though, as with any new environment, there are a few idiosyncrasies to deal with.
Love the One You’re With
Android natively knows three fonts, by the shorthand names of “sans”, “serif”, and “monospace”. These fonts are actually the Droid series of fonts, created for the Open Handset Alliance by Ascender.[10]
For those fonts, you can just reference them in your layout XML, if you so choose. The following layout from the sample project shows example code, and can also be found in the Source Code area at http://apress.com:
This layout builds a table showing short samples of four fonts. Notice how the first three have the attribute, whose value is one of the three built‑in font faces (e.g., “sans”).
The three built‑in fonts are very nice. However, it may be that a designer, or a manager, or a customer wants a different font than one of those three. Or perhaps you want to use a font for specialized purposes, such as a “dingbats” font instead of a series of PNG graphics.
The easiest way to accomplish this is to package the desired font(s) with your application. To do this, simply create an folder in the project root, and put your TrueType (TTF) fonts in the assets. You might, for example, create and put your TTF files in there.
Then, you need to tell your widgets to use that font. Unfortunately, you can no longer use layout XML for this, since the XML does not know about any fonts you may have tucked away as an application asset. Instead, you need to make the change in Java code:
Дата добавления: 2015-05-16; просмотров: 738;