最近遇到一个问题,在使用自定义控件的时候,做全球化翻译的时候,中文和英文的字数不一样,导致界面显示,中文正常,英文就显示有问题。
中文两个字,翻译成英文就两个单词,再加上单词间的空格,两个中文的地方完全显示不下英文翻译,所以英文需要换行,尝试了加转义字符\n,也是不会换行。
//画文字
Paint.FontMetrics fontMetrics = paint.getFontMetrics();
paint.setTextSize(textSize);
paint.setColor(Color.parseColor("#FFFFFF"));
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(0);
canvas.drawText(“要显示的子串。。。”, x, y, paint);
X,Y是要显示的子串的坐标位置。。。。
这么写的话,中文是可以显示成功,但是英文超出了我要显示的区域,这时,我要修改显示的规则,让英文可以换行
TextPaint textPaint = new TextPaint();
textPaint.setColor(Color.parseColor("#ffffff"));
textPaint.setTextSize(textSize);
textPaint.setAntiAlias(true);
StaticLayout layoutopen = new StaticLayout("要显示的子串", textPaint, (int) 300 , Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);
//这里的参数300,表示字符串的长度,当满300时,就会换行,也可以使用“\r\n”来实现换行
canvas.save();
canvas.translate(x, y);
layoutopen.draw(canvas);
canvas.restore();
这样就能让英文子串在超过显示的宽度后,换行显示了