站内搜索: 请输入搜索关键词
当前页面: 图书首页 > Programming Wireless Devices with the Java2 Platform

Programming Wireless Devices with the Java2 Platform

[ directory ] Previous Section Next Section

10.3 ImageItem

Image objects can be added to a Form either directly as an Image or as an ImageItem. ImageItem is a simple class that wraps an Image. By default, the ImageItem is placed next to the previous StringItem or ImageItem. If an ImageItem is wider than the horizontal space available in the row, it is placed in a new row. Images wider than the Form width are clipped.

The ImageItem can be given a preferred layout policy with the setLayout method. The directive passed to this method defines whether the image is centered within the Form margins (LAYOUT_CENTER), left-justified (LAYOUT_LEFT), or right-justified (LAYOUT_RIGHT) and whether a forced line break is created before or after the image (LAYOUT_NEWLINE_BEFORE, LAYOUT_NEWLINE_AFTER). The layout is merely a hint for the device and might not be supported by the device. It also varies from device to device how the alignment directives are actually implemented. In some devices LAYOUT_LEFT might cause the ImageItem to float to the left margin, allowing nearby StringItems to wrap around the side of the image.

Continuing the example started in Section 9.4, "Form," the following code adds an ImageItem to the Form.

graphics/10inf04.gif
Image image = Image.createImage(
    "/images/PhotoAlbum.png");
ImageItem imageItem =
    new ImageItem(null, // (no label)
        image,
        ImageItem.LAYOUT_CENTER |
        ImageItem.LAYOUT_NEWLINE_AFTER |
        ImageItem.LAYOUT_NEWLINE_BEFORE,
        "(preview image)");
form.append(imageItem);

graphics/new_icon.gif

There is also a LAYOUT_2 directive that causes an ImageItem to follow the strictly specified MIDP 2.0 Form layout rules (discussed in Section 10.9, "Form Layout"). This means that the horizontal alignment directives LAYOUT_LEFT, LAYOUT_RIGHT, and LAYOUT_CENTER will behave the same way as in other Item classes. If LAYOUT_2 is not set, then the results are (for backwards compatibility reasons) implementation-specific since MIDP Specification version 1.0 did not define the behavior of the layout directives clearly for ImageItem.

    [ directory ] Previous Section Next Section