我看过很多处理类似问题的帖子,但没有一个对我有用。在 Canvas
中,我有一个大小为 200px x 200px 的矩形,我想在这个矩形中写入文本。文本不需要填满整个矩形,但重要的是当它到达矩形的末端时应该自动换行。我如何在 Android 中执行此操作?
最佳答案
您可以使用 StaticLayout .
RectF rect = new RectF(....)
StaticLayout sl = new StaticLayout("This is my text that must fit to a rectangle", textPaint, (int)rect.width(), Layout.Alignment.ALIGN_CENTER, 1, 1, false);
canvas.save();
canvas.translate(rect.left, rect.top);
sl.draw(canvas);
canvas.restore();
关于安卓编程 : How to draw multiline text in a rectangle?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12920794/