黑莓开发中的manager

我不太喜欢中文的翻译叫做黑莓,怪怪的。还是比较喜欢叫Bb, baby, my baby, hehe

 

今天比较懒惰,随意转一篇吧,我比较喜欢这个例子,不懂英文也没关系,看到例子照用就是,本来觉得manager比较麻烦和神秘,最近用的多了,觉得很方便,帮你个性化布局,看看吧:

Managers

 

The next level beneath Screen in the BlackBerry UI heirarchy is the Manager class. Managers are responsible for vertical and/or horizontal scrolling, and the positioning and layout of fields. Fields are added to managers which will place it in an appropriate area on the BlackBerry device's screen. The BlackBerry API contains a set of managers that extend from the Manager class and provide a layout mechanism for common screen designs.

HorizontalFieldManager

 

  • Lays out fields from left to right in a single row. This manager can provide horizontal scrolling for fields that do not fit on the BlackBerry device screen as well as vertical scrolling for fields that are taller than the screen.

VerticalFieldManager

 

  • Lays out fields in a single vertical column. This manager can provide vertical scrolling for fields that do not fit on the BlackBerry device screen as well as horizontal scrolling for fields that are wider than screen.

FlowFieldManager

 

  • Lays out fields in a horizontal then vertical flow. Fields are positioned from left to right, and any fields that do not fit within the allotted horizontal space are placed on the following line, starting from the left. The FlowFieldManager also supports horizontal and vertical scrolling.

DialogFieldManager

 

  • Handles an icon, a message, and a special area which can hold a list of user-specified custom fields. A VerticalFieldManager is used to layout the fields in the user area. It lays out its icon in the top left corner, and its message label in the top left corner.

Managers are an extension of the Field class and can contain other managers. This is known as nesting. Nesting can be used to create enhanced layout styles such as a column or table layout. The following is a sample that is designed to divide a BlackBerry deviuce screen into two even columns:

//Create an instance of MainScreen.
MainScreen theScreen = new MainScreen();

//Create a HorizontalFieldManager to hold the
//two VerticalFieldManagers.
HorizontalFieldManager backGround = new HorizontalFieldManager();
VerticalFieldManager leftColumn = new VerticalFieldManager();
VerticalFieldManager rightColumn = new VerticalFieldManager();

//The data to be displayed.
String[] names = {
"Homer",
"Marge",
"Bart",
"Lisa",
"Maggie"};
int ages[] = {36, 34, 10, 8, 3};

//Arrays of fields to display the names
//and ages.
LabelField displayNames[] = new LabelField[5];
LabelField displayAges[] = new LabelField[5];

int count, width, spaceSize;
StringBuffer sBuffer = new StringBuffer();

//Add the column titles.
LabelField leftTitle = new LabelField("Name");
leftColumn.add(leftTitle);

LabelField rightTitle = new LabelField("Age");
rightColumn.add(rightTitle);

//Determine half of the screen width.
width = Graphics.getScreenWidth() / 2;

//Get the default font.
Font font = Font.getDefault();

//Determine the size of a space in the
//default font.
spaceSize = font.getAdvance(' ');

//Build up a string that contains enough spaces
//that it fills half of the screen. This will be
//used to set the column width.
for(count = 0;count <= width;count += spaceSize)
{
sBuffer.append(' ');
}

//Add spacers to move to the next line and set
//the column width.
leftColumn.add(new LabelField(sBuffer.toString()));
rightColumn.add(new LabelField(sBuffer.toString()));

//Add some data to the screen.
//Populate and add the name and age fields.
for (count = 0; count < 5; count++)
{
displayNames[count] = new LabelField(names[count]);
displayAges[count] = new LabelField(String.valueOf(ages[count]));

//Add the name LabelField to the left column.
leftColumn.add(displayNames[count]);

//Add the age LabelField to the right column.
rightColumn.add(displayAges[count]);
}

//Add the two vertical columns to the
//horizontal field manager.
backGround.add(leftColumn);
backGround.add(rightColumn);

//Add the horizontal field manager to
//the screen.
theScreen.add(backGround);

The output of the sample code above will appear as two columns of text as shown below:

The Manager class can be extended to allow custom behaviour. This lets you position fields in a unique manner, or add a graphical effect to the screens in your application. The following is an example of a custom manager that works with a custom field that will be discussed later in this article. The custom field allows you to set its x and y coordinates used to position the field in the manager. The custom manager below makes use of these x and y coordinates to determine where to draw the field.

public class CustomManager extends Manager
{
public CustomManager()
{
//Disable scrolling in this manager.
super(Manager.NO_HORIZONTAL_SCROLL |
Manager.NO_VERTICAL_SCROLL);
}

//Override sublayout.
protected void sublayout(int width, int height) {
CustomField field;

//Get the total number of fields within
//this manager.
//Note that this manager only supports the
//CustomField described within this article.
int numberOfFields = getFieldCount();

for (int i = 0; i < numberOfFields; i++)
{
//Get the field.
field = (CustomField)getField(i);

//Obtain the custom x and y coordinates for
//the field and set the position for
//the field.
setPositionChild(field, field.getXCoord(), field.getYCoord());

//Layout the field.
layoutChild(field, width, height);
}
//Set the manager's extent
setExtent(width, height);
}

public int getPreferredWidth()
{
//This manager is designed to utilize the
//entire screen width.
return Graphics.getScreenWidth();
}

public int getPreferredHeight()
{
//This manager is designed to utilize the
//entire screen height.
return Graphics.getScreenHeight();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值