自定义列表字段无法正确绘制
我使用从这里获得的 BSimpleTable 添加了一个自定义列表字段组件: http://javaandjava.blogspot.com/2010/02 /simple-table-component-for-blackberry.html 不幸的是,该组件不支持其内部字段的右对齐,因此我尝试通过修改代码来添加它,如下所示:
public class BSimpleRowRenderer extends Manager{
private int[] colWidthPercents;
private int[] cellFrontPaddings;
private int preferredHeight;
public BSimpleRowRenderer(long style,Field[] rowContents,int preferredHeight,
int[] colWidthPercents,int[] cellFrontPaddings){
super(style);
for (int col = 0; col < rowContents.length; col++) {
add(rowContents[col]);
}
this.colWidthPercents = new int[rowContents.length];
for(int i=0;i<colWidthPercents.length;i++){
this.colWidthPercents[i] = colWidthPercents[i];
}
this.cellFrontPaddings = new int[rowContents.length];
for(int i=0;i<cellFrontPaddings.length;i++){
this.cellFrontPaddings[i] = cellFrontPaddings[i];
}
this.preferredHeight = preferredHeight;
}
protected void sublayout(int width, int height) {
int x=0;
int totalWidth = Display.getWidth();
for (int col = 0; col < getFieldCount(); col++) {
// Set the size and position of the current cell.
Field curCellField = getField(col);
XYPoint offset = calcAlignmentOffset(curCellField, Math.max(0,colWidthPercents[col]*totalWidth/100), Math.max(0, getPreferredHeight()));
layoutChild(curCellField, width-x,getPreferredHeight());
setPositionChild(curCellField, x+offset.x, 0);
x+=(int)Math.floor((colWidthPercents[col]*totalWidth)/100);
}
setExtent(Display.getWidth(), getPreferredHeight());
}
public void drawRow(Graphics g, int x, int y, int width, int height) {
layout(width, height);
setPosition(x, y);
g.pushRegion(getExtent());
subpaint(g);
g.popContext();
}
public int getPreferredWidth() {
return Display.getWidth();
}
public int getPreferredHeight() {
return preferredHeight;
}
private XYPoint calcAlignmentOffset(Field field, int width, int height)
{
XYPoint offset = new XYPoint(0, 0);
long fieldStyle = field.getStyle();
long field_x_style = fieldStyle & Field.FIELD_HALIGN_MASK;
if (field_x_style == Field.FIELD_RIGHT){
offset.x = width - field.getExtent().width;
}
else if (field_x_style == Field.FIELD_HCENTER){
offset.x = (width - field.getExtent().width) / 2;
}
long field_y_style = fieldStyle & Field.FIELD_VALIGN_MASK;
if (field_y_style == Field.FIELD_BOTTOM){
offset.y = height - field.getExtent().height;
}
else if (field_y_style == Field.FIELD_VCENTER){
offset.y = (height - field.getExtent().height) / 2;
}
return offset;
}
}
问题是,右对齐仅适用于突出显示的行或已突出显示的行内的字段突出显示。 我想知道我在上面的代码中哪里做错或遗漏了。 我现在真的很绝望,因为任何修改都不能反映我想要完成的任务。
I added a custom listfield component using BSimpleTable which I got from here :
http://javaandjava.blogspot.com/2010/02/simple-table-component-for-blackberry.html
Unfortunately the component doesn't support right alignment for the fields inside it, so I tried to added it by modifying the code, like this :
public class BSimpleRowRenderer extends Manager{
private int[] colWidthPercents;
private int[] cellFrontPaddings;
private int preferredHeight;
public BSimpleRowRenderer(long style,Field[] rowContents,int preferredHeight,
int[] colWidthPercents,int[] cellFrontPaddings){
super(style);
for (int col = 0; col < rowContents.length; col++) {
add(rowContents[col]);
}
this.colWidthPercents = new int[rowContents.length];
for(int i=0;i<colWidthPercents.length;i++){
this.colWidthPercents[i] = colWidthPercents[i];
}
this.cellFrontPaddings = new int[rowContents.length];
for(int i=0;i<cellFrontPaddings.length;i++){
this.cellFrontPaddings[i] = cellFrontPaddings[i];
}
this.preferredHeight = preferredHeight;
}
protected void sublayout(int width, int height) {
int x=0;
int totalWidth = Display.getWidth();
for (int col = 0; col < getFieldCount(); col++) {
// Set the size and position of the current cell.
Field curCellField = getField(col);
XYPoint offset = calcAlignmentOffset(curCellField, Math.max(0,colWidthPercents[col]*totalWidth/100), Math.max(0, getPreferredHeight()));
layoutChild(curCellField, width-x,getPreferredHeight());
setPositionChild(curCellField, x+offset.x, 0);
x+=(int)Math.floor((colWidthPercents[col]*totalWidth)/100);
}
setExtent(Display.getWidth(), getPreferredHeight());
}
public void drawRow(Graphics g, int x, int y, int width, int height) {
layout(width, height);
setPosition(x, y);
g.pushRegion(getExtent());
subpaint(g);
g.popContext();
}
public int getPreferredWidth() {
return Display.getWidth();
}
public int getPreferredHeight() {
return preferredHeight;
}
private XYPoint calcAlignmentOffset(Field field, int width, int height)
{
XYPoint offset = new XYPoint(0, 0);
long fieldStyle = field.getStyle();
long field_x_style = fieldStyle & Field.FIELD_HALIGN_MASK;
if (field_x_style == Field.FIELD_RIGHT){
offset.x = width - field.getExtent().width;
}
else if (field_x_style == Field.FIELD_HCENTER){
offset.x = (width - field.getExtent().width) / 2;
}
long field_y_style = fieldStyle & Field.FIELD_VALIGN_MASK;
if (field_y_style == Field.FIELD_BOTTOM){
offset.y = height - field.getExtent().height;
}
else if (field_y_style == Field.FIELD_VCENTER){
offset.y = (height - field.getExtent().height) / 2;
}
return offset;
}
}
The problem is, the right alignment is only working for fields inside highlighted row or row which have been highlighted.
I wonder where have I done wrong or missed in the above code.
I'm in real desperate now as to any modification doesn't reflect what I want to accomplish.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您需要表格,那么您可以尝试以下操作:如何 - 使用 TableLayoutManager 创建丰富的 UI 布局。它对我来说非常有效。
If table is what you need, then you could try this: How To - Create a rich UI layout with TableLayoutManager. It worked for me perfectly.