Align BlackBerry HorizontalFieldManager

Horizontally, I want to display two bitmaps, and a label field is displayed between them. The code seems simple, but all fields are added to the left of the screen.

HorizontalFieldManager hfm = new HorizontalFieldManager();

callbmp = new BitmapField(ei.getBitmap(),Field.FOCUSABLE |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);
+3
source share
3 answers

HorizontalFieldManager exposes the fields from left to right in the order in which they are added. Style bits for horizontal layout are ignored.

If you want the left, right and center in a horizontal line, you will need a custom manager.

+4
source
 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")));
+6
source

This should be your requirement:
enter image description here
This can be done simply by subtracting the width of the elements that you add to your horizontal field manager. By default, leftButtoneither the first item added to the HFM will be added on the left. Then you can add your label ( userName) rightButtonas follows:

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);
+2
source

Source: https://habr.com/ru/post/1774904/


All Articles