2012-05-21 80 views
0

我只想知道如何更改ListField的物品背景顏色。我的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

+0

您可以發佈您想要的'ListField'圖像。 – Rupak

回答

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(); 
    } 
} 
+0

我會理解代碼並檢查出來。 –

+0

某處我知道我只是需要玩fillrectangle方法,但我無法得到所需的結果,感謝張貼乾淨整潔的例子,關於項目背景顏色對於這個愚蠢的問題抱歉,理解了這個代碼後,我知道它只是設置顏色的一部分。 –

0

如果你需要改變聚焦狀態的背景顏色比你的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); 
    } 
} 
+0

我試過這個,但它不工作,我不明白什麼是焦點的意義,並沒有把重點放在這裏,我只需要設置項目的背景顏色。 –

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

}

+0

其實我想讓我的listitems自定義(如更改背景顏色)代碼,你粘貼似乎設置整個列表的背景。 –

+0

我覺得我從Rajkiran得到了同樣的東西,但它不工作。 –