开发者

Add vertical spacing between fields on VerticalFieldManager

I am adding fields to a Ver开发者_如何转开发ticalFieldManager. Is there a method of adding vertical spacing between fields?


There's several solutions to this, one being that you can create a custom field to be used as a spacer between your other fields.

private static class SpacerField extends Field
{
    private int spacerWidth;
    private int spacerHeight;
    private SpacerField(int width, int height) {
        spacerWidth = width;
        spacerHeight = height;
    }

    protected void layout(int width, int height) {
        setExtent(getPreferredWidth(), getPreferredHeight());
    }

    protected void paint(Graphics g) {
        // nothing to paint; this is a blank field
    }

    public int getPreferredHeight() {
        return spacerHeight;
    }

    public int getPreferredWidth() {
        return spacerWidth;
    }
}

//...
// Usage

add(new LabelField("Before Spacer"));
add(new SpacerField(0, 100));
add(new LabelField("After Spacer"));

Setting the padding or margins of your contained fields is another solution. It's up to you on what you think is the best way of managing things.


There are more elogant ways of doing this using the setPositionChild() methods but a simple work around is to give your fields padding using the setPadding(int top, int right, int bottom, int left) method.

myField.setPadding(5, 0, 5, 0);


I actually find that most times, what you want to do is call setMargin() on the vertical field manager's child fields, not sePadding(). I certainly think creating a SpacerField is now unnecessarily complex.

myField.setMargin(5, 0, 5, 0);

It depends on which kind of child field we're talking about, and how its size is specified, but in general, setPadding() will actually make the field bigger, which could have a visual impact, depending on how the field's background and border are drawn. setMargin() does not make the field any bigger, which I think is more consistent with what the question is asking.

See Mr Vincenzo's answer here, with pictures!

Note: setMargin() and setPadding() were officially added to the Field API relatively late in the BlackBerry Java evolution, but were available as undocumented methods before that.

Another Note: I have seen strange problems when using setMargin(), like the one described in this question. In that case, I did have to resort to one of the other two solutions here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜