水平居中垂直场管理器中的字段

时间:2012-01-03 03:29:59

标签: blackberry

vfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL);
    vfm.add(new LabelField("horizontally centered...",Field.FIELD_HCENTER | LabelField.FOCUSABLE));
    vfm.add(new LabelField("horizontally centered...",Field.FIELD_HCENTER | LabelField.USE_ALL_WIDTH | LabelField.FOCUSABLE));

    add(vfm);

为什么我不能让我的字段水平对齐。我尝试过不同的组合,但不能让一个labelfield集中。如果我在USE_ALL_WIDTH下面添加第二个字段,那么第一个字段将居中。

我不知道这样做的正确方法!

编辑:

按照下面提供的链接,我尝试了:

vfm = new VerticalFieldManager(Field.USE_ALL_WIDTH | Field.USE_ALL_HEIGHT){


        protected void sublayout( int width, int height ) {

            super.sublayout( width, height );

            width = getWidth();
            height = getHeight();

            for (int i = 0;i < this.getFieldCount() - 1; i++)
            {
                System.out.println("field:" + i);
                Field field = this.getField(i);
                //this positions the item in the middle of the manager
                int x = (int)((width - field.getWidth()) * 0.50);
                setPositionChild(field, x, field.getTop());
            }
        }


    };


    vfm.add(new LabelField("Facebook"));


    add(vfm);

问题在于我没有得到任何字段。我该如何实施呢?

3 个答案:

答案 0 :(得分:10)

以下是BlackBerry上的对齐规则:

  • HorizontalFieldManager只能对齐垂直中的字段。因此,在创建这些字段时,只有以下样式(也称为对齐位)具有任何效果:FIELD_TOP,FIELD_VCENTER,FIELD_BOTTOM。例如new LabelField("My field", Field.Field_VCENTER)

Horizo​​ntalFieldManager示例

HorizontalFieldManager example

  • VerticalFieldManager只能对齐水平中的字段。只有以下样式有效:FIELD_LEFT,FIELD_HCENTER,FIELD_RIGHT。

VerticalFieldManager示例

enter image description here

水平和垂直对齐示例

这是一个垂直和水平对齐屏幕中心按钮的示例:

public class AlignmentScreen extends MainScreen{

        public AlignmentScreen(){

            //A MainScreen has a VerticalFieldManager to lay out its 
            //fields from top to bottom, the style bits here tell it
            //to span the whole width of the screen and not to scroll
            super(Manager.USE_ALL_WIDTH | Manager.NO_VERTICAL_SCROLL);

            //Set the VerticalFieldManager to have a GREEN background
            getMainManager().setBackground(BackgroundFactory.createSolidBackground(0xFF00FF00));

            //Create a new HorizontalFieldManager which is centered horizontally
            //and spans the whole height of the containing VerticalFieldManager
            HorizontalFieldManager hfm = new HorizontalFieldManager(Field.FIELD_HCENTER | Manager.USE_ALL_HEIGHT);

            //Set the HorizontalFieldManager's background to RED
            hfm.setBackground(BackgroundFactory.createSolidBackground(0xFFFF0000));

            //Create a button and center align it vertically
            ButtonField button = new ButtonField("Test", Field.FIELD_VCENTER);

            //Add the button to the HoriztonalFieldManager
            hfm.add(button);

            //Add the HorizontalFieldManager to the VerticalFieldManager
            add(hfm);
        }
    }

屏幕应如下所示:

Centering vertically and horizontally

您应该能够修改上述内容,以使字段符合您的需要。

答案 1 :(得分:2)

首先将 Field 添加到 HorizontalFieldManager (比如hfm),然后添加 hfm vfm ,我希望这可以解决您的问题:

VerticalFieldManager vfm = new VerticalFieldManager();
HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.FIELD_HCENTER);
hfm.add(new LabelField("My Label"));
add(hfm);

请参阅以下链接以查找 An effective way of aligning fields in a VerticalFieldManager

答案 2 :(得分:2)

检查此代码段。它不一定是 compicated

public final class CenterScreen extends MainScreen {
    public CenterScreen() {        
        LabelField lbl1 = new LabelField("First and Foremost", Field.FIELD_HCENTER);
        lbl1.setBorder(BorderFactory.createRoundedBorder(new XYEdges(10, 10, 10, 10),
            Color.BLUE, Border.STYLE_SOLID));

        LabelField lbl2 = new LabelField("Second", Field.FIELD_HCENTER);
        lbl2.setBorder(BorderFactory.createRoundedBorder(new XYEdges(10, 10, 10, 10),
            Color.RED, Border.STYLE_SOLID));

        LabelField lbl3 = new LabelField("Last but not least", Field.FIELD_HCENTER);
        lbl3.setBorder(BorderFactory.createRoundedBorder(new XYEdges(10, 10, 10, 10),
            Color.GREEN, Border.STYLE_SOLID));

        VerticalFieldManager vfm = new VerticalFieldManager(Field.USE_ALL_WIDTH);        
        vfm.add(lbl1);
        vfm.add(lbl2);
        vfm.add(lbl3);
        add(vfm);    
    }
}


导致 enter image description here