我该如何布局这样的组件?

时间:2012-04-05 07:11:51

标签: java blackberry java-me

在BlackBerry(OS 6+)中,我需要一个如下图所示的布局。但到目前为止,我还没有能够得到红圈部分。

我使用了HorizontalFieldManager并添加了BasicEditField和两个ButtonFields。但是只有BasicEditField在屏幕上可见,并且两个按钮被推出屏幕(但它们仍然是可聚焦的,尽管不可见)。

所以任何人的想法?任何帮助非常感谢!

图像(我正在尝试做的是红色圆圈): enter image description here

3 个答案:

答案 0 :(得分:3)

试试这个它正在发挥作用。

HorizontalFieldManager manager = new HorizontalFieldManager(USE_ALL_WIDTH);
    BasicEditField editField = new BasicEditField("Test", ""){
        protected void layout(int width, int height) {
            super.layout(width, height);
            setExtent(140, getPreferredHeight());
        }
    };
    manager.add(editField);
    manager.add(new ButtonField("button1"));
    manager.add(new ButtonField("button2"));
    add(manager);

在上面的setExtent(width,height)方法中,根据需要设置宽度。

答案 1 :(得分:2)

您可以查看课程MyManager。使用HorizontalFieldManager可以实现相同的布局,但在使用HorizontalFieldManager时我失败了几次。

public class MyScreen extends MainScreen {
    private BasicEditField _bef = new BasicEditField();
    private ButtonField _bf1 = new ButtonField("Button");
    private ButtonField _bf2 = new ButtonField("Button");

    public MyScreen() {
        MyManager mgr = new MyManager();
        _bef.setText("BasicEditField");
        mgr.add(_bef);
        mgr.add(_bf1);
        mgr.add(_bf2);
        add(mgr);
    }
}

class MyManager extends Manager {
    private final int HORIZONTAL_GAP = 5;

    public MyManager() {
        super(0);
    }

    protected void sublayout(int width, int height) {
        int fields = getFieldCount();
        Field field;
        int nHeight = 0;
        int remaininigWidth = width;
        int x0, x1, x2;

        // specific layout implementation only
        if (fields == 3) {
            // right most ButtonField
            field = getField(2);
            layoutChild(field, remaininigWidth, height);
            nHeight = Math.max(nHeight, field.getHeight()); 
            remaininigWidth -= (field.getWidth() + HORIZONTAL_GAP);
            x2 = remaininigWidth;

            // middle ButtonField
            field = getField(1);
            layoutChild(field, remaininigWidth, height);
            nHeight = Math.max(nHeight, field.getHeight()); 
            remaininigWidth -= (field.getWidth() + HORIZONTAL_GAP);
            x1 = remaininigWidth;

            // first BasicEditField
            field = getField(0);
            layoutChild(field, remaininigWidth, height);
            nHeight = Math.max(nHeight, field.getHeight());
            x0 = 0;

            setPositionChild(getField(0), x0, (nHeight - getField(0).getHeight()) / 2);
            setPositionChild(getField(1), x1, (nHeight - getField(1).getHeight()) / 2);
            setPositionChild(getField(2), x2, (nHeight - getField(2).getHeight()) / 2);

            setExtent(width, nHeight);
        } else {
            setExtent(width, 0);
        }

        field = null;
    }
}

答案 2 :(得分:1)

 HorizontalFieldManager hfm = new HorizontalFieldManager();
BasicEditField editField = new BasicEditField("", ""){
    protected void layout(int width, int height) {
        super.layout(100,  getPreferredHeight());
        setExtent(100, getPreferredHeight());
    }
};
hfm.add(editField);
hfm.add(new ButtonField("button1"));
hfm.add(new ButtonField("button2"));
add(hfm);
相关问题