2012-01-03 76 views
5
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); 

的问题是,我没有得到任何领域。我应该如何实施它?

+0

的Add您的labelField在'HFM',并尝试将其添加到你的'VFM' .. – BBdev 2012-01-03 03:41:24

回答

2

首先添加FieldHorizontalFieldManager(比如HFM),然后添加hfmvfm,我希望这将解决您的问题:

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

+0

谢谢,但你的方法是不是为我工作。我会检查链接的方法。我不能相信这么简单的东西太可笑了 – Adam 2012-01-03 06:20:02

+0

在BB中对齐字段有时真的很复杂.BTW,您试过哪种设备?我已经在BB 9800火炬上测试过这个,看起来很完美。 – 2012-01-03 06:54:47

+0

我正在研究9700.我会尝试在我的绘画方法中计算 – Adam 2012-01-03 15:28:18

10

以下是BlackBerry对齐规则:

  • A 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

你应该能修改上面让你的领域对准您想要的方式。

+0

Nice One。但是我可以将VerticalFieldManager居中,因为您拥有测试按钮字段的中心?我的问题是我在垂直领域管理器中有一些领域,我想垂直居中该VerticalFieldManager。帮助我,如果你有它的溶解。 – 2012-12-26 12:58:02

+0

请不要在评论中发布问题,在StackOverflow上发布新问题。 – donturner 2013-01-02 10:35:51

+0

@亚当如果这个答案帮助了你,请考虑接受它 – donturner 2014-01-11 14:28:04

2

查看此代码段。它不一定非得是complicated

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