[转]How to – Create a custom layout manager for a screen – 千丝海阁

首页 » 黑莓开发 » [转]How to – Create a custom layout manager for a screen

[转]How to – Create a custom layout manager for a screen

1535 0

Procedure

You can create your own custom layout manager by extending the net.rim.device.api.ui.Manager class and customizing it to fit your needs. Custom layout managers enable you to customize the placement of fields on the screen and customize navigation within these fields. To do this, you need to implement at least the following three methods:

public int getPreferredWidth()

This method returns the manager’s preferred width.
public int getPreferredHeight()

This method returns the manager’s preferred height.
public void sublayout(int height, int width)

This method is invoked by the manager’s layout() method and instructs each child field how and where to be laid out.

This can be accomplished by calling Manager.setPositionChild(Field field, int x, int y) and LayoutChild(Field field, int width, int height) for each field contained in the manager.

Here is an example of how a screen can associate itself with a custom layout manager:

/*********************
Layout Manager
*********************/
class LayoutManager extends Manager {
public LayoutManager() {
//construct a manager with vertical scrolling
super(Manager.VERTICAL_SCROLL);
}
//overwrite the nextFocus method for custom navigation
protected int nextFocus(int direction, boolean alt) {
//retrieve the index of the current field that is selected
int index= this.getFieldWithFocusIndex();
if(alt) {
if(direction 0){...}
else{...}
}
// if we did not handle it, let the manager's parent class
if (index == this.getFieldWithFocusIndex())
return super.nextFocus(direction, alt);
else
return index;
}
protected void sublayout(int width, int height) {
Field field;
//get total number of fields within this manager
int numberOfFields = getFieldCount();
int x = 0;
int y = 0;
for (int i = 0;i < numberOfFields;i++) {
field = getField(i); //get the field
setPositionChild(field,x,y); //set the position for the field
layoutChild(field, width, height); //lay out the field
y += ...;
...
}
setExtent(width, height);
}
public int getPreferredWidth() {
return 160;
}
public int getPreferredHeight() {
int height= 0;
int numberOfFields= getFieldCount();
for (int i= 0; i < numberOfFields; i++) {
height+= getField(i).getPreferredHeight();
return height;
}
}
/****************
Main Class
****************/
...
RichTextField myField = new RichTextField("Hello");
RichTextField myOtherField = new RichTextField("World");
LayoutManager myManager = new LayoutManager();
MainScreen myScreen = new MainScreen();
myScreen.add(myManager);
myManager.add(myField);
myManager.add(myOtherField);
...

文章评分1次,平均分5.0



上一篇:自定义Field,paint background后光标消失的另类解决方法
下一篇:获取设备内安装的程序清单

本文原始地址:http://www.tiandiyoyo.com/2011/04/%e8%bd%achow-to-create-a-custom-layout-manager-for-a-screen/
本站所有文章,除了特别注明外,均为本站原创,转载请注明出处来自www.tiandiyoyo.com

您可能还会对以下文章感兴趣:

评论前先开启评论开关: