Android 你如何包装文本?

Android 你如何包装文本?,android,canvas,android-layout,Android,Canvas,Android Layout,嘿,这里-我正在创建一个实时墙纸,其中包含通过TextPaint和DynamicClayout对象直接绘制到画布上的文本。在我的DynamicClayOut对象中,我将宽度设置为画布宽度,现在正在寻找一种方法来包装延伸到画布之外的文本。以下是我的文本设置代码: //token is my large text string TextPaint tp = new TextPaint(); Layout sl = new DynamicLayout(token, tp, (i

嘿,这里-我正在创建一个实时墙纸,其中包含通过TextPaint和DynamicClayout对象直接绘制到画布上的文本。在我的DynamicClayOut对象中,我将宽度设置为画布宽度,现在正在寻找一种方法来包装延伸到画布之外的文本。以下是我的文本设置代码:

    //token is my large text string
    TextPaint tp = new TextPaint();
    Layout sl = new DynamicLayout(token, tp, (int) canvasWidth, Layout.Alignment.ALIGN_NORMAL, 0, 0, true);
    canvas.translate(startPositionX , startPositionY);
    sl.draw(canvas);

如果此文本超出画布宽度,我如何包装它?感谢您的帮助

几个小时后得到这个。。。查看StringTokenizer,从我的字符串中查找最长的单词,并将剩余文本包装到该宽度

// Assume s contains a string of words
String longestWord = "";
StringTokenizer st = new StringTokenizer(s, " ,\t");
while (st.hasMoreTokens()) {
    String w = st.nextToken();
    if (w.length() > longestWord.length()) {
        longestWord = w;
    }
}

float textWidth = tp.measureText(longestWord+" ");
Layout sl = new DynamicLayout(token, tp, (int) textWidth, Layout.Alignment.ALIGN_CENTER, lineSpacing, 0, true);