ListField项目背景颜色

时间:2012-05-21 09:22:57

标签: blackberry

我只想知道如何更改ListField的项目背景颜色。我的ListField中有两个项目,就像这个一样。

| First One | Second One ................. |

我需要改变第一个背景颜色。

我的drawListRow(..)方法看起来像这样

public void drawListRow(ListField listField, Graphics graphics, 
                                    int index, int y, int width) {
    int oldColor = 0;
    try {
        oldColor = graphics.getColor();
        String txt = (vector.elementAt(index)).toString();
        int xPos = 15;
        int yPos = 5 + y;

        //graphics.clear();
        graphics.setColor(Color.GREEN);
        graphics.fillRect(0, y, (Display.getWidth()*10/100), yPos);

        graphics.drawText(txt, xPos, yPos);
        //graphics.fillRect(0,(index*Display.getHeight()/10),Display.getWidth(),Display.getHeight()/10);
    } finally {
        graphics.setColor(oldColor);
    }
}

但这不起作用。

enter image description here

3 个答案:

答案 0 :(得分:2)

虽然你附上了一张照片,但我仍感到困惑。图像没有回答一些问题,例如,它在一行中看起来如何聚焦(我实际上并不理解)。

但您可以查看以下输出和代码。我想如果您检查代码,可以根据需要自定义外观。

生成输出

enter image description here

如何使用

public class MyScreen extends MainScreen {
    private Vector listElements;

    public MyScreen() {
        setTitle("Custom ListField Demo");

        // data for the ListField
        listElements = new Vector();
        for (int i = 0; i < 4; i++) {
            listElements.addElement("Some text for row " + i);
        }
        ListField taskList = new ListField() {
            // disable default focus drawing
            protected void drawFocus(Graphics graphics, boolean on) {
            };
        };
        taskList.setCallback(new ListCallback(listElements));
        taskList.setSize(listElements.size());
        taskList.setRowHeight(40);
        add(taskList);
    }
}

ListCallback实施

class ListCallback implements ListFieldCallback {
    final int COLOR_INDEX_NORMAL_BG = 0x1D6789;
    final int COLOR_INDEX_FOCUSED_BG = 0x0E8CB3;
    final int COLOR_NORMAL_BG = 0x2A2A2A;
    final int COLOR_FOCUSED_BG = 0x1F1F1F;

    private Vector listElements;
    public ListCallback(Vector listElements) {
        this.listElements = listElements;
    }

    public void drawListRow(ListField list, Graphics graphics, int index, int y,
            int width) {
        int rowHeight = list.getRowHeight(index);
        boolean isSelectedRow = (list.getSelectedIndex() == index);
        int indexBgColor = isSelectedRow ? COLOR_INDEX_FOCUSED_BG : COLOR_INDEX_NORMAL_BG;
        int rowBgColor = isSelectedRow ? COLOR_FOCUSED_BG : COLOR_NORMAL_BG;

        final int indexWidth = width / 10;

        // draw row background
        fillRectangle(graphics, rowBgColor, 0, y, width, rowHeight);
        // draw index background
        fillRectangle(graphics, indexBgColor, 0, y, indexWidth, rowHeight);

        // set text color, draw text
        Font font = list.getFont();

        graphics.setColor(Color.WHITE );
        graphics.setFont(font);

        String indexText = "" + (index + 1);
        String textToDraw = "";
        try {
            textToDraw = (String) listElements.elementAt(index);
        } catch (Exception exc) {
        }

        int xText = (indexWidth - font.getAdvance(indexText)) / 2;
        int yText = (rowHeight - font.getHeight()) / 2;
        graphics.drawText(indexText, xText, y + yText, 0, indexWidth);

        final int margin = 5;
        int availableWidth = (width - indexWidth) - 2 * margin;
        xText = indexWidth + margin;
        yText = (rowHeight - font.getHeight()) / 2;
        graphics.drawText(textToDraw, xText, y + yText, DrawStyle.ELLIPSIS, availableWidth); 
    }

    private void fillRectangle(Graphics graphics, int color, int x, int y, int width, int height) {
        graphics.setColor(color);
        graphics.fillRect(x, y, width, height);
    }

    public Object get(ListField list, int index) {
        // not implemented
        return "";
    }

    public int indexOfList(ListField list, String prefix, int string) {
        // not implemented
        return 0;
    }

    public int getPreferredWidth(ListField list) {
        return Display.getWidth();
    }
}

答案 1 :(得分:0)

如果需要更改onFocus背景颜色,请在ListField上添加drwFocus方法。

protected void drawFocus(Graphics graphics, boolean on) {
        //get the focus rect area
        XYRect focusRect = new XYRect();
        getFocusRect(focusRect);

    boolean oldDrawStyleFocus = graphics.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS);
    try {
        if (on) {
            //set the style so the fields in the row will update its color accordingly
            graphics.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, true);
            int oldColour = graphics.getColor();
            try {
                graphics.setColor(0xc8d3db); //set the color and draw the color
                graphics.fillRect(focusRect.x, focusRect.y,
                        focusRect.width, focusRect.height);
            } finally {
                graphics.setColor(oldColour);
            }
            //to draw the row again
            drawListRow(this, graphics, getSelectedIndex(),
                    focusRect.y, focusRect.width);
            //              drawRow(graphics, focusRect.x,focusRect.y, focusRect.width,focusRect.height);
        }

    } finally {
        graphics.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus);
    }
}

答案 2 :(得分:0)

检查编辑的答案,

    protected void drawFocus(Graphics graphics, boolean on) {
    XYRect focusRect = new XYRect();
    getFocusRect(focusRect);

   boolean oldDrawStyleFocus = graphics.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS);
     try {

if(on){

        graphics.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, true);

        int oldColour = Color.BLACK;

     try {

        graphics.fillRect(focusRect.x, focusRect.y,
                              focusRect.width, focusRect.height);
        } finally {
            graphics.setColor(oldColour);
        }
        //to draw the row again
        drawListRow(this, graphics, getSelectedIndex(),
                    focusRect.y, focusRect.width);
    }

} finally {
  graphics.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus);
}

}