Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何把一个句子包装成多行?_Android - Fatal编程技术网

Android 如何把一个句子包装成多行?

Android 如何把一个句子包装成多行?,android,Android,在这个链接的帮助下,我能够输入文本和它的工作状态 但是如何将这个巨大的句子包装成多行呢 我主要需要这部分代码的帮助 paint.setStyle(Paint.Style.FILL); //turn antialiasing on paint.setAntiAlias(true); paint.setTextSize(25); canvas.drawText("Style.FILL.show the full complete tex

在这个链接的帮助下,我能够输入文本和它的工作状态

但是如何将这个巨大的句子包装成多行呢

我主要需要这部分代码的帮助

paint.setStyle(Paint.Style.FILL);
        //turn antialiasing on
        paint.setAntiAlias(true);
        paint.setTextSize(25);
        canvas.drawText("Style.FILL.show the full complete text Thanks dude..I am searching for this thing since last three days. But no where they have given as simple as you given ", 75, 110, paint);
把整个句子放在一行,但我想看多行

怎么可能呢

请帮帮我
提前感谢

有很多选择

一种是用“+”分隔:

另一种方法是将字符串放入strings.xml中。这样,代码中就不会有“垃圾”,另外还可以为何时使用每个字符串添加限定符(例如本地化)


编辑:我以为你的意思是它在代码中惹恼了你。现在我知道你需要它在应用程序中工作

下一个代码将获取任何文本,并根据您给定的矩形使其具有文字换行。如果文本太长,即使对于指定的最大行数,文本也将被截断。它还支持文本对齐

代码如下:

// TODO make this whole class more customizable (text size, text color,gravity...), maybe using textView
// TODO make this code better somehow. the positioning is very weird.
final Rect rect = ...;
// find the truncated text to show based on the max lines that are allowed:
StaticLayout sl;
int pivot, maxCharactersCount = mTextToShow.length(), minCharactersCount = 0;
sl = new StaticLayout(mTextToShow, mTextPaint, rect.width(), Alignment.ALIGN_NORMAL, 1, 1, false);
int lineCount = sl.getLineCount();
if (lineCount > mMaxLines)
    while (true) {
        pivot = (maxCharactersCount + minCharactersCount) / 2;
        final String text = mTextToShow.substring(0, pivot);
        sl = new StaticLayout(text, mTextPaint, rect.width(), Alignment.ALIGN_NORMAL, 1, 1, false);
        lineCount = sl.getLineCount();
        if (lineCount <= mMaxLines) {
            minCharactersCount = pivot;
            if (maxCharactersCount <= minCharactersCount + 1)
                break;
        } else
            maxCharactersCount = pivot;
    }
if (lineCount == 0)
    return;
// get the bounding width of the text (of all lines):
int maxTextWidth = 0;
for (int i = 0; i < lineCount; ++i)
    maxTextWidth = (int) Math.max(maxTextWidth, sl.getLineWidth(i));
// some initializations...
final float textHeight = mTextPaint.getTextSize();
final float totalTextHeight = textHeight * lineCount;
final float rotation = getRotation();
final String truncatedText = sl.getText().toString();
// calculate where to start the drawing:
final float yCenter = (rect.bottom + rect.top) / 2;
final float yStart = yCenter - totalTextHeight / 2;
int startX;
switch (mAlign) {
case CENTER:
    startX = (rect.left + rect.right) / 2;
    break;
case RIGHT:
    startX = rect.right;
    break;
case LEFT:
default:
    startX = rect.left;
    break;
}
// start drawing:
canvas.save();
if (rotation != 0)
    canvas.rotate(rotation);
canvas.translate(startX, yStart);
// for each line, draw it in the corresponding location, based on its width and which line it is:
for (int i = 0; i < lineCount; ++i) {
    final int lineTextWidth = (int) sl.getLineWidth(i);
    final String lineText = truncatedText.substring(sl.getLineStart(i), sl.getLineEnd(i));
    int xToDrawRelativeToStart = 0;
    switch (mAlign) {
    case CENTER:
        xToDrawRelativeToStart = -lineTextWidth / 2;
        break;
    case RIGHT:
        xToDrawRelativeToStart = -lineTextWidth;
        break;
    case LEFT:
    default:
        xToDrawRelativeToStart = 0;
        break;
    }
    canvas.drawText(lineText, xToDrawRelativeToStart, textHeight * (i + 1), mTextPaint);
}
canvas.restore();
//要使整个类更加可自定义(文本大小、文本颜色、重力…),可以使用textView
//如何使代码变得更好。位置很奇怪。
最终Rect Rect=。。。;
//根据允许的最大行数查找要显示的截断文本:
静态布局sl;
int pivot,maxCharacterScont=mTextToShow.length(),minCharacterScont=0;
sl=新的静态布局(mTextToShow、mTextPaint、rect.width()、Alignment.ALIGN_NORMAL、1、1、false);
int lineCount=sl.getLineCount();
如果(行数>mMaxLines)
while(true){
pivot=(MaxCharacterScont+MinCharacterScont)/2;
最终字符串text=mTextToShow.substring(0,pivot);
sl=新的静态布局(文本、mTextPaint、rect.width()、Alignment.ALIGN_NORMAL、1、1、false);
lineCount=sl.getLineCount();

如果(lineCount您可以在代码中手动拆分文本(如其他人所建议),但问题是在IDE中,您看到的文本是单空格字体,而用于绘制文本的字体不是单空格字体,因此线条将不相等:

这是一位杰出的运动员,他坐在车上

Lorem ipsum door sit amet,连续驾驶精英。车辆酒后驾驶不合格。

你看到了区别


我建议您使用。

您可以使用“\n”每次你想断线时,我从DB获取文本,那么我如何才能每次为每个字段\n给出结果???@Yume117:即使我看不到输出中的9个字符,我也认为你的意思是代码中的9个字符看起来很烦人。如果你想让它在android应用程序上工作,你需要添加“\n”行间。例如:String t=“hello\nworld”;但它可能不适用于绘画,因此您需要自己分割字符串,并在每行之后移动画布。是否希望我向您展示一些代码?我甚至使其能够具有重力……好的,我发布了一些代码。这应该会对您有所帮助,因为这是一个类似的问题。
// TODO make this whole class more customizable (text size, text color,gravity...), maybe using textView
// TODO make this code better somehow. the positioning is very weird.
final Rect rect = ...;
// find the truncated text to show based on the max lines that are allowed:
StaticLayout sl;
int pivot, maxCharactersCount = mTextToShow.length(), minCharactersCount = 0;
sl = new StaticLayout(mTextToShow, mTextPaint, rect.width(), Alignment.ALIGN_NORMAL, 1, 1, false);
int lineCount = sl.getLineCount();
if (lineCount > mMaxLines)
    while (true) {
        pivot = (maxCharactersCount + minCharactersCount) / 2;
        final String text = mTextToShow.substring(0, pivot);
        sl = new StaticLayout(text, mTextPaint, rect.width(), Alignment.ALIGN_NORMAL, 1, 1, false);
        lineCount = sl.getLineCount();
        if (lineCount <= mMaxLines) {
            minCharactersCount = pivot;
            if (maxCharactersCount <= minCharactersCount + 1)
                break;
        } else
            maxCharactersCount = pivot;
    }
if (lineCount == 0)
    return;
// get the bounding width of the text (of all lines):
int maxTextWidth = 0;
for (int i = 0; i < lineCount; ++i)
    maxTextWidth = (int) Math.max(maxTextWidth, sl.getLineWidth(i));
// some initializations...
final float textHeight = mTextPaint.getTextSize();
final float totalTextHeight = textHeight * lineCount;
final float rotation = getRotation();
final String truncatedText = sl.getText().toString();
// calculate where to start the drawing:
final float yCenter = (rect.bottom + rect.top) / 2;
final float yStart = yCenter - totalTextHeight / 2;
int startX;
switch (mAlign) {
case CENTER:
    startX = (rect.left + rect.right) / 2;
    break;
case RIGHT:
    startX = rect.right;
    break;
case LEFT:
default:
    startX = rect.left;
    break;
}
// start drawing:
canvas.save();
if (rotation != 0)
    canvas.rotate(rotation);
canvas.translate(startX, yStart);
// for each line, draw it in the corresponding location, based on its width and which line it is:
for (int i = 0; i < lineCount; ++i) {
    final int lineTextWidth = (int) sl.getLineWidth(i);
    final String lineText = truncatedText.substring(sl.getLineStart(i), sl.getLineEnd(i));
    int xToDrawRelativeToStart = 0;
    switch (mAlign) {
    case CENTER:
        xToDrawRelativeToStart = -lineTextWidth / 2;
        break;
    case RIGHT:
        xToDrawRelativeToStart = -lineTextWidth;
        break;
    case LEFT:
    default:
        xToDrawRelativeToStart = 0;
        break;
    }
    canvas.drawText(lineText, xToDrawRelativeToStart, textHeight * (i + 1), mTextPaint);
}
canvas.restore();