我有一个TextView tv
,我在上面写了一些文字。然后从TextView中提取Bitmap,然后在画布上绘制此Bitmap。我希望TextView中的文本在垂直和水平方向都居中,我使用tv.setGravity(Gravity.CENTER)。
然而,文本最终只是水平居中。
以下是创建TextView并设置所有参数的代码:
tv.layout(0, 0, displayWidth, displayHeight);
tv.setTextSize(60);
tv.setTextColor(Color.WHITE);
tv.setBackgroundColor(Color.GREEN);
tv.setGravity(Gravity.CENTER);
tv.setText(text);
tv.setDrawingCacheEnabled(true);
textBmp = tv.getDrawingCache().copy(Bitmap.Config.ARGB_8888, true);
tv.setDrawingCacheEnabled(false);
以下是我如何将它绘制到我的画布中
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(textBmp, 0, 0, null);
}
}
我必须做什么才能让文字垂直居中。
答案 0 :(得分:0)
您可能会遇到以下问题:
tv.layout(0, 0, displayWidth, displayHeight);
layout功能的文档说明如下:
public void layout (int l, int t, int r, int b)
Parameters:
l : Left position, relative to parent
t : Top position, relative to parent
r : Right position, relative to parent
b : Bottom position, relative to parent
所以,你要求你的TextView
与父母的左上角和左边没有距离。