2011-04-19 45 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