2011-04-19 47 views
0

我添加使用BSimpleTable定制listfield组件,我从这里得到: http://javaandjava.blogspot.com/2010/02/simple-table-component-for-blackberry.html 不幸的是,部分不支持它里面的字段右对齐,所以我试图通过修改添加它代码,就像这样:定制Listfield不画正确

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

如果表是你需要的,那么你可以试试这个:How To - Create a rich UI layout with TableLayoutManager。它完美地为我工作。

+0

感谢您的回答,但更改组件是我想要做的最后一件事,除了缺少正确的对齐支持外,当前组件已足够好。 无论如何,谢谢。 – 2011-04-27 14:24:59