BlackBerry HorizontalFieldManager alignment
Horizontally, I want to display two bitmaps, and between them display a label field. The code seems straightforward, but all the fields are added on the left side of the screen.
HorizontalFieldManager hfm = new HorizontalFieldManager();
callbmp = new BitmapField(ei.getBitmap(),Field.FOCUSAB开发者_开发问答LE |BitmapField.FIELD_LEFT);
LabelField NAME = new LabelField("mylable", LabelField.FIELD_HCENTER);
mailbmp = new BitmapField(mail.getBitmap(),Field.FOCUSABLE|BitmapField.FIELD_RIGHT);
hfm.add(callbmp);
hfm.add(NAME);
hfm.add(mailbmp);
add(hfm);
Manager customManager = new Manager(0)
{
protected void sublayout(int width, int height) {
setPositionChild(
getField(0),
0,
0);
layoutChild(
getField(0),
getField(0).getPreferredWidth(),
getField(0).getPreferredHeight());
setPositionChild(
getField(1),
Graphics.getScreenWidth()/2 - getField(1).getPreferredWidth()/2,
0);
layoutChild(
getField(1),
getField(1).getPreferredWidth(),
getField(1).getPreferredHeight());
setPositionChild(
getField(2),
Graphics.getScreenWidth() - getField(2).getPreferredWidth(),
0);
layoutChild(
getField(2),
getField(2).getPreferredWidth(),
getField(2).getPreferredHeight());
setExtent(width, height);
}
};
customManager.add(new BitmapField(Bitmap.getBitmapResource("image1.png")));
customManager.add(new LabelField("Hello Alignment"));
customManager.add(new BitmapField(Bitmap.getBitmapResource("image2.png")));
HorizontalFieldManager lays out the fields left-to-right in the order in which they are added. The style bits for horizontal layout are ignored.
If you want left, right and center on a horizontal line, you'll need a custom manager.
This should be your requirement:
It can be done simply by subtracting the widths of the items you are adding on your horizontal field manager. By default the
leftButton
or the first item you add on the HFM will be added on left. Then you can add your label(userName
) and the rightButton
in the following way:LabelField userName = new LabelField("MaheshBabu");
HorizontalFieldManager horizontalBar = new HorizontalFieldManager(USE_ALL_WIDTH|Manager.NO_HORIZONTAL_SCROLL|Manager.NO_HORIZONTAL_SCROLLBAR);
horizontalBar.setBackground(BackgroundFactory.createSolidBackground(Color.BLACK));
rightButton.setMargin(0, 0, 0, Display.getWidth()-rightButton.getPreferredWidth()-leftButton.getPreferredWidth()-userName.getPreferredWidth());
horizontalBar.add(leftButton);
horizontalBar.add(userName);
horizontalBar.add(rightButton);
精彩评论