0

我使用从这里获得的 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;
}

}

问题是,右对齐仅适用于突出显示的行或已突出显示的行内的字段。我想知道我在上面的代码中哪里做错或遗漏了。我现在真的很绝望,因为任何修改都不能反映我想要完成的事情。

4

1 回答 1

1

如果表格是您需要的,那么您可以试试这个:如何 - 使用 TableLayoutManager 创建丰富的 UI 布局。它非常适合我。

于 2011-04-22T08:00:14.480 回答