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

Ada*_*dam 5 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);
Run Code Online (Sandbox Code Playgroud)

为什么我不能让我的字段水平对齐.我尝试过不同的组合,但不能让一个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);
Run Code Online (Sandbox Code Playgroud)

问题是我没有得到任何领域.我该如何实施呢?

don*_*ner 10

以下是BlackBerry上的对齐规则:

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

Horizo​​ntalFieldManager示例

Horizo​​ntalFieldManager示例

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

VerticalFieldManager示例

在此输入图像描述

水平和垂直对齐示例

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

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

屏幕应如下所示:

垂直和水平居中

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


Tar*_*sim 2

首先将您的添加Field到 a HorizontalFieldManager(例如 hfm),然后将其添加hfmvfm,我希望这能解决您的问题:

VerticalFieldManager vfm = new VerticalFieldManager();
HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.FIELD_HCENTER);
hfm.add(new LabelField("My Label"));
add(hfm);
Run Code Online (Sandbox Code Playgroud)

请参阅以下链接以查找对齐字段的有效方法VerticalFieldManager