自定义列表字段无法正确绘制

时间:2011-04-19 16:49:58

标签: blackberry alignment listfield

我使用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;
}

}

问题是,右对齐仅适用于突出显示的突出显示的行或行内的字段。 我想知道在上面的代码中我做错了什么或错过了什么。 我现在非常绝望,因为任何修改都不能反映我想要完成的任务。

1 个答案:

答案 0 :(得分:1)

如果您需要表格,那么您可以尝试:How To - Create a rich UI layout with TableLayoutManager。它非常适合我。