嗨,大家正在使用画布绘制文本,我需要每隔一小时更改一次textview, 如果textview扩展了宽度,它应该显示在下一行 例如,我有一个像#34; Good Morning"和#34;早安rachel!欢迎你"
第一个textview早上好会在单行显示,但第二个textview我需要用两行打印 我该怎么画呢
String mytext = "hi how are you how was the day "
canvas.drawText(mytext , x, centerX, mTextPaint);
答案 0 :(得分:2)
scale = resources.getDisplayMetrics().density;
TextPaint TextPaint=new TextPaint();
TextPaint.setARGB(255, 255, 255, 255);
TextPaint.setTextSize(28f);
TextPaint.setTypeface(myfonttime);
StaticLayout mTextLayout;
mTextLayout = new StaticLayout(myText, TextPaint, canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
int textHeight = mTextLayout.getHeight()- (int) (16 * scale +5.0);
int textWidth = canvas.getWidth() - (int) (16 * scale);
float x = cWidth / 2f - textWidth/2f - bounds.left;
float y = cHeight / 2f - textHeight/2f;
canvas.save();
canvas.translate(x, y);
mTextLayout.draw(canvas);
canvas.restore();
答案 1 :(得分:0)
例如你有像这样的字符串
String text = "This is\nmulti-line\ntext";
然后在这样的textview中绘制
canvas.drawText("This is", 100, 100, mTextPaint);
canvas.drawText("multi-line", 100, 150, mTextPaint);
canvas.drawText("text", 100, 200, mTextPaint);
第二种方式。
TextPaint mTextPaint=new TextPaint();
StaticLayout mTextLayout = new StaticLayout(mText, mTextPaint, canvas.getWidth(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
canvas.save();
// calculate x and y position where your text will be placed
textX = ...
textY = ...
canvas.translate(textX, textY);
mTextLayout.draw(canvas);
canvas.restore();
有关详情,请参阅此link