blackberry - 在垂直字段管理器中将字段水平居中

标签 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);

问题是我没有得到任何字段。我应该如何实现呢?

最佳答案

以下是BlackBerry上的对齐规则:

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

  • Horizo​​ntalFieldManager示例

  • 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);
            }
        }
    

    屏幕应如下所示:

    您应该能够修改上面的内容,以使字段与所需的方式保持一致。

    关于blackberry - 在垂直字段管理器中将字段水平居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8707608/

    相关文章:

    java - 实现带有最终字段的接口(interface)还是直接访问字段?

    android - 适用于 iOS + Android + BlackBerry 的 testflight 等服务

    blackberry - 开发黑莓应用

    javascript - 黑莓纹波模拟器 : how to work with sql lite?

    javascript - 黑莓浏览器 Javascript 时间 DST 问题

    黑莓推送通知实现

    java - jar 文件与黑莓不兼容

    java - 获取 BlackBerry 设备的默认语言

    c++ - BB10 - 启用 Qml 调试。仅在安全环境中使用

    java - 将 http 响应读入输入流时遇到问题