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)
HorizontalFieldManager示例
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/