2012-07-19 42 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) + "..."; 

} 

使用名称字符串变量。