Horizontal centering of fields in a vertical field manager

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

Why can't I get my fields to be aligned horizontally. I tried different combinations, but I can not get a single laboratory element to be centered. If I add a second field below with USE_ALL_WIDTH, then the first field will be centered.

I don’t know how to do it right!

EDIT:

Following the link below, I tried to do:

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

The problem is that I am not getting any fields. How should I implement it?

+5
source share
3 answers

Here are the alignment rules for BlackBerry:

  • A HorizontalFieldManager . ( ): FIELD_TOP, FIELD_VCENTER, FIELD_BOTTOM. new LabelField("My field", Field.Field_VCENTER)

HorizontalFieldManager

HorizontalFieldManager example

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

, , .

+10

Field HorizontalFieldManager (, hfm), hfm > vfm, , :

VerticalFieldManager vfm = new VerticalFieldManager();
HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.FIELD_HCENTER);
hfm.add(new LabelField("My Label"));
add(hfm);

. , VerticalFieldManager

+2

Check out this piece of code. It does not have to be compiled.

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


As a result enter image description here

+2
source

All Articles