2

我已经建立了一个列表并在每个单元格中插入了标签。现在,太长的文本会消失。我想包装文本,使其在每个单元格内完全可见。

你能帮忙吗?

更新:问题已解决

对于那些需要答案的人,我在容器中使用了 LWUIT 的 HTMLComponent。HTMLComponent 允许您使用 HTML 代码。这将允许您按照您希望的方式格式化您的列表。


这是有关解决方案的更多详细信息。

在带有 LWUIT 的 Java ME 中,我使用 HTMLComponent 来获得我想要的精确布局。对我来说最好的方法是在 HTMLComponent 中使用 HTML Table。它的行为就像 HTML。

String html_code = "";

html_code  = "<table width='100%'>";
html_code += "<tr><td><strong>"+fullname+"</strong></td></tr>";
if (title.length()>0) { html_code += "<tr><td><i>"+title+"</i></td></tr>"; }
if (message.length()>0) { html_code += "<tr><td>"+message+"</td></tr>"; }
if (date.length()>0) { html_code += "<tr><td><i>"+date+"</i></td></tr>"; }
html_code += "</table>";       
             
HTMLComponent html = new HTMLComponent(null);
html.setBodyText(html_code);
4

1 回答 1

1

如果您正在寻找更“优雅”的解决方案,我在网上找到了一个方便的资源。我在这里发布以供参考,但 HtmlComponent 可以完成这项工作。

    import com.sun.lwuit.Font;

/** A class supporting word wrap for MIDP. */

public class WordWrap {

Font font;
int width;
String txt;
int pos;

/**
* Initializes the WordWrap object with the given Font, the text string
* to be wrapped, and the target width.
*
* @param font: The Font to be used to calculate the character widths.
* @param txt: The text string to be wrapped.
* @param width: The line width.
*/

public WordWrap (Font font, String txt, int width) {

this.font = font;
this.txt = txt;
this.width = width;
}

/**
* returns the next line break position. If no text is left, -1 is returned.
*/

public int next () {

int i = pos;
int len = txt.length ();

if (pos >= len) return -1;

int start = pos;

while (true) {
while (i < len && txt.charAt (i) > ' ')
i++;

int w = font.stringWidth (txt.substring (start, i));
if (pos == start) {
if (w > width) {
while (font.stringWidth (txt.substring (start, --i)) > width)
{ }
pos = i;
break;
}
}

if (w <= width) pos = i;

if (w > width || i >= len || txt.charAt(i) == '\n') break;
i++;
}

return pos >= len ? pos : ++pos;
}

}




import com.sun.lwuit.Button;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.events.FocusListener;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.plaf.Border;
import com.sun.lwuit.plaf.Style;

/**
 *
 * @author rubycube
 */
public class WrapList extends Container {

    private Button hiddenButton;
    private int id;

    public WrapList(String text, int containerID) {
        id = containerID;
        this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        this.setFocusable(false);
        final Style thisContainerStyle = this.getStyle();
        Border thisContainerBorder = Border.createRoundBorder(20, 20, 0xcccccc);
        thisContainerStyle.setBorder(thisContainerBorder);

        hiddenButton = new Button(" ");
        hiddenButton.setPreferredSize(new Dimension(1, 1));
        Style style = hiddenButton.getStyle();
        style.setBgTransparency(0, false);
        style.setBorder(Border.createEmpty());

        FocusListener hiddenButtonFL = new FocusListener() {

            public void focusGained(Component cmp) {

                WrapList parentContainer = ((WrapList) (cmp.getParent()));
                Border parentContainerBorder = Border.createRoundBorder(20, 20, 0xff6600);
                Style parentContainerStyle = parentContainer.getStyle();
                parentContainerStyle.setBorder(parentContainerBorder);
                parentContainerStyle.setBgColor(0xff9900);
                parentContainerStyle.setBgTransparency(50);
                parentContainer.repaint();
            }

            public void focusLost(Component cmp) {
                WrapList parentContainer = ((WrapList) (cmp.getParent()));
                Border parentContainerBorder = Border.createRoundBorder(20, 20, 0xcccccc);
                Style parentContainerStyle = parentContainer.getStyle();
                parentContainerStyle.setBorder(parentContainerBorder);
                parentContainerStyle.setBgTransparency(0);
                parentContainer.repaint();
            }
        };
        hiddenButton.addFocusListener(hiddenButtonFL);

        Label l = new Label(text);
        l.setSelectedStyle(thisContainerStyle);
        //l.setUnselectedStyle(thisContainerStyle);
        WordWrap ww = new WordWrap(l.getStyle().getFont(), text, (Display.getInstance().getDisplayWidth() - 10));
        int si = 0;
        int ei = 0;
        while (true) {
            int np = ww.next();
            if (np == -1) {
                break;
            } else {
                si = ei;
                ei = np;
            }
            String lineText = text.substring(si, ei);
            Label line = new Label(lineText);
            line.setEndsWith3Points(false);
            this.addComponent(line);
        }
        this.addComponent(hiddenButton);
    }

    public void addActionListener(ActionListener actionlistener) {
        hiddenButton.addActionListener(actionlistener);
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }
}
于 2012-08-30T20:33:47.507 回答