0

我需要制作新闻列表,所以我用一个拇指和两个文本实现了自定义列表字段,它用对象绘制。一切都很好,给了我预期的结果,但是文本换行graphics有一些问题我基本上是 android 开发人员在那里是文件允许自动设置文本文件,但在这种情况下,第二个文本绑定一个文本。我从这里推荐了客户列表字段wrap content

这是屏幕::

在此处输入图像描述

代码::

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

更新:

新画面 ::在此处输入图像描述

4

1 回答 1

1

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

}

使用名称字符串变量代替 graphics.drawText。

于 2012-07-20T08:52:14.193 回答