0

我只想知道如何更改ListField项目背景颜色。我有两件ListField像这样的东西。

|第一个|第二个........|

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

我的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);
    }
}

但这不起作用。

在此处输入图像描述

4

3 回答 3

2

尽管您附上了图片,但我仍然感到困惑。该图像没有回答某些问题,例如,它将如何集中显示在一行上(我实际上不明白)。

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

生成的输出

在此处输入图像描述

如何使用

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();
    }
}
于 2012-05-22T08:54:36.783 回答
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);
    }
}
于 2012-05-21T09:48:28.177 回答
0

检查编辑的答案,

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

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

如果(开){

        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);
}

}

于 2012-05-21T09:50:50.173 回答