【问题标题】:Horizontally centering fields in a vertical field manager在垂直字段管理器中水平居中字段
【发布时间】:2012-01-03 03:29:59
【问题描述】:
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);

为什么我的字段不能水平对齐。我尝试了不同的组合,但无法使单个标签字段居中。如果我在下面使用 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);

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

【问题讨论】:

  • HFM 中添加您的标签字段并尝试将其添加到您的VFM ..

标签: blackberry


【解决方案1】:

以下是 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);
        }
    }

屏幕应该是这样的:

您应该能够修改上述内容以使您的字段以您想要的方式对齐。

【讨论】:

  • 不错的一个。但是我可以将 VerticalFieldManager 居中吗,因为您将测试按钮字段居中?我的问题是我在垂直字段管理器中有一些字段,我想垂直居中该垂直字段管理器。如果你有它,请帮助我。
  • 请不要在 cmets 中发布问题,在 StackOverflow 上发布新问题。
  • @Adam 如果这个答案对你有帮助,请考虑接受它
【解决方案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

【讨论】:

  • 谢谢,但你的方法对我不起作用。我会检查链接的方法。我不敢相信这么简单的事情竟然复杂得可笑
  • 在 BB 中对齐字段有时真的很复杂。顺便说一句,您在哪个设备上尝试过?我已经在 BB 9800 Torch 上进行了测试,看起来很完美。
  • 我正在研究 9700。我会尝试在我的绘画方法中计算
【解决方案3】:

检查此代码 sn-p。不必那么复杂

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


导致

【讨论】:

    猜你喜欢
    • 2011-11-20
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 2012-02-04
    相关资源
    最近更新 更多