自定义列表字段的换行文本

时间:2012-07-19 09:49:31

标签: blackberry

我需要制作新闻列表,所以我用一个拇指和两个文本实现了自定义列表字段,并用graphics对象绘制。所有都很好,并给出了我期望的结果但是有一些问题< strong>文本包装我基本上是android开发人员,并且有wrap content文件允许自动设置textfiled但在这种情况下有一个文本超过秒。我推荐了客户列表字段from here

这是screen ::

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

1 个答案:

答案 0 :(得分:1)

你需要以编程方式进行,我们在列表中没有标签字段,因为我们使用图形渲染它。 所以我曾经做过一个解决方法来做到这一点,我计算了我的自定义字体文本的可用像素数,然后我会用三个周期符号给出多少空间。 您可以使用此代码,如果它可以帮助您在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) + "...";

}

使用name string变量代替graphics.drawText。