setMargin method in BlackBerry

点点圈 提交于 2020-01-04 09:12:09

问题


I have to use setMargin() method in my Blackberry application. Would any one please explain me this method in detail?

setMargin(top,right,bottom,left)

How can I use above 4 parameters to display my controls on the screen? Maybe its a very simple thing to understand but I am really very confused with the top right bottom left parameters. Maybe I am misinterpreting it and getting improper display of controls on screen.


回答1:


By default field managers are aligned from left to right and from top to bottom but it is possible to change this. setMargin is relative to the field or to an object in the field on its alignment.

Bitmap bitmap = EncodedImage.getEncodedImageResource("img/image.png").getBitmap();

HorizontalFieldManager hfm = new HorizontalFieldManager();
BitmapField a = new BitmapField(bitmap);
a.setMargin(20,0,0,10);
hfm.add(a);

BitmapField b = new BitmapField(bitmap);
b.setMargin(20,0,0,10);
hfm.add(b);

add(hfm);

In the above example bitmap 'a' is positioned 20 pixels relative to the top of the field manager hfm. E.g. if this is the only field manager it is positioned 20 pixes from the top of the screen. It is positioned 10 pixels from the left of the start of hfm.

Bitmap 'b' is also positioned 20 pixels from the top of hfm and it is positioned 10 pixels from the right most pixel of bitmap 'a'. It's because we are in a (left to right) horizontal field that b is relative to a's right most pixel.

We could flip this example from right to left by realigning the manager. I.e.

HorizontalFieldManager hfm = new HorizontalFieldManager(Field.FIELD_RIGHT);
...
a.setMargin(0,10,0,0); // positioned 10 pixels from the right

Or similarly a vertical manager

VerticalFieldManager vfm = new VerticalFieldManager();
...
a.setMargin(20,10,0,0);
...
b.setMargin(20,10,0,0);  
...

The above 'a' is aligned 20 pixels from the top of vfm and 10 pixels from the left of vfm. 'b' is aligned 10 pixels from the left of vfm and 20 pixels from the bottom of 'a'



来源:https://stackoverflow.com/questions/4526251/setmargin-method-in-blackberry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!