2012-07-19 39 views
0

我需要做的新聞列表,所以我有一個拇指和兩個文本實現自定義listfield並將其與graphics畫object.it都走了罰款,給我造成的預期,但也有一些問題, 文本包木窗我基本上是Android開發者和有wrap content文件允許設置自動textfiled但在這種情況下,有超過第二個文本綁定。我有指客戶listfield from here文本換行

這裏是屏幕::

enter image description here

代碼::

public class CustomListField extends ListField implements ListFieldCallback { 

    private Vector _listData; 

    private int _MAX_ROW_HEIGHT = 80; 

    public CustomListField(Vector data) { 

     _listData = data; 

     setSize(_listData.size()); 

     setSearchable(true); 

     setCallback(this); 

     setRowHeight(_MAX_ROW_HEIGHT); 

    } 

    protected void drawFocus(Graphics graphics, boolean on) { 

     XYRect rect = new XYRect(); 

     graphics.setGlobalAlpha(150); 

     graphics.setColor(Color.BLUE); 

     getFocusRect(rect); 

     drawHighlightRegion(graphics, HIGHLIGHT_FOCUS, true, rect.x, rect.y, 
       rect.width, rect.height); 

    } 

    public int moveFocus(int amount, int status, int time) { 

     this.invalidate(this.getSelectedIndex()); 

     return super.moveFocus(amount, status, time); 

    } 

    public void onFocus(int direction) { 

     super.onFocus(direction); 

    } 

    protected void onUnFocus() { 

     this.invalidate(this.getSelectedIndex()); 

    } 

    public void refresh() { 

     this.getManager().invalidate(); 

    } 

    public void drawListRow(ListField listField, Graphics graphics, int index, 
      int y, int w) { 

     ListRander listRander = (ListRander) _listData.elementAt(index); 

     graphics.setGlobalAlpha(255); 

     graphics.setFont(Font.getDefault().getFontFamily().getFont(Font.PLAIN, 
       24)); 

     final int margin = 5; 

     final Bitmap thumb = listRander.getListThumb(); 

     final String listHeading = listRander.getListTitle(); 

     final String listDesc = listRander.getListDesc(); 

     final String listDesc2 = listRander.getListDesc2(); 

     final Bitmap nevBar = listRander.getNavBar(); 

     // list border 

     graphics.setColor(Color.GRAY); 

     graphics.drawRect(0, y, w, _MAX_ROW_HEIGHT); 

     // thumbnail border & thumbnail image 

     graphics.setColor(Color.BLACK); 

     graphics.drawRoundRect(margin - 2, y + margin - 2, 
       thumb.getWidth() + 2, thumb.getHeight() + 2, 5, 5); 

     graphics.drawBitmap(margin, y + margin, thumb.getWidth(), thumb 
       .getHeight(), thumb, 0, 0); 

     // drawing texts 

     //graphics.setFont(FontGroup.fontBold); 

     graphics.drawText(listHeading, 2 * margin + thumb.getWidth(), y 
       + margin); 

     graphics.setColor(Color.GRAY); 

     //graphics.setFont(FontGroup.smallFont); 

     graphics.drawText(listDesc, 2 * margin + thumb.getWidth(), y + margin 
       + 20); 

     graphics.drawText(listDesc2, 2 * margin + thumb.getWidth(), y + margin 
       + 32); 

     // draw navigation button 

     final int navBarPosY = y 
       + (_MAX_ROW_HEIGHT/2 - nevBar.getHeight()/2); 

     final int navBarPosX = Graphics.getScreenWidth() - nevBar.getWidth() 
       + margin; 

     graphics.drawBitmap(navBarPosX, navBarPosY, nevBar.getWidth(), nevBar 
       .getHeight(), nevBar, 0, 0); 

    } 

    public Object get(ListField listField, int index) { 

     String rowString = (String) _listData.elementAt(index); 

     return rowString; 

    } 

    public int indexOfList(ListField listField, String prefix, int start) { 

     for (Enumeration e = _listData.elements(); e.hasMoreElements();) { 

      String rowString = (String) e.nextElement(); 

      if (rowString.startsWith(prefix)) { 

       return _listData.indexOf(rowString); 

      } 

     } 

     return 0; 

    } 

    public int getPreferredWidth(ListField listField) { 

     return 3 * listField.getRowHeight(); 
    } 
} 

/* 
* protected boolean trackwheelClick (int status, int time) { 
* 
* invalidate(getSelectedIndex()); 
* 
* Dialog.alert(" U have selected :" + getSelectedIndex()); 
* 
* return super.trackwheelClick(status, time); 
* 
* } 

更新:

新的屏幕:: enter image description here

+0

歡迎來到Stack Overflow。請閱讀[如何問](http://stackoverflow.com/questions/how-to-ask),[你有什麼嘗試?](http://mattgemmell.com/2008/12/08/what-have - 你試過/),[如何提出問題的智能方式](http://catb.org/esr/faqs/smart-questions.html)。 – 2012-07-19 09:50:46

+0

我將在這裏發佈的代碼,請刪除評論 – 2012-07-19 09:52:00

+0

使用的FontFamily fontFamily中[] = FontFamily.getFontFamilies減小字體大小(); Font font = fontFamily [1] .getFont(FontFamily.CBTF_FONT,15); – 2012-07-19 10:37:02

回答

1

你需要做的方案,我們沒有標籤字段列表中,因爲我們正在繪製的圖形使用它。 所以我用來做一個解決辦法這樣做,我計算沒有像素availabale爲我的文字自定義字體,它就會多大的存儲空間,我將有三個時期的跡象給。 您可以使用此代碼,如果,它可以幫助你,在地方graphics.drawText的使用下drawListRow方法

String name =(String)ht.get("title"); 
xTotal= f2.getAdvance(name); 
xAvail= Display.getWidth() - <bitmap>.getWidth() - 30; 
if(xTotal > xAvail) 
{ 
forLabel= name.length() * xAvail/xTotal ; 
    name = name.substring(0, forLabel - 3) + "..."; 

} 

使用名稱字符串變量。