0

ListFieldのアイテムの背景色を変更する方法を知りたいだけです。私はListFieldこのような私のアイテムを2つ持っています。

|最初の 1 つ |2 番目の 1 つ.................|

最初の背景色を変更する必要があります。

私の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 {

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

}

于 2012-05-21T09:50:50.173 に答える