2016-02-06 56 views
0

嗨伙计们正在使用画布绘制文本,并且如果textview延伸了它应显示在下一行中的宽度,我需要每隔一小段更改textview, 例如,我有一个文本如“早上好”和“早安瑞秋!欢迎你”Android Wear(Long Textview)

第一TextView的早安将被正确地显示在单行但第二TextView的,我需要两线 打印我怎样才能画它

String mytext = "hi how are you how was the day " 
    canvas.drawText(mytext , x, centerX, mTextPaint); 

回答

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(); 
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