Android canvas drawText - 如何底部对齐文本?

时间:2017-06-14 18:21:48

标签: android android-canvas drawtext

我有一个自定义视图,它应该绘制一些底部对齐的文本。 文字大小应为视图高度的50%。

我应该如何更改此代码才能正常工作?

@Override
protected void onDraw(Canvas canvas)
{
    float h = getMeasuredHeight();
    float textHeight = h*0.5f;
    paint.setTextSize(textHeight);
    String str = "Abcdefghijklm";

    paint.getTextBounds(str, 0, str.length(), bounds);
    float height = bounds.height();
    float yPos = height;
    canvas.drawText(str, 0, yPos, paint);
}

1 个答案:

答案 0 :(得分:0)

将您的yPos替换为:

v <- c(Decisive=2, Substantial=3, Positive=40, None=20)
paste0(names(v), "\n", v, "%")
# [1] "Decisive\n2%"    "Substantial\n3%" "Positive\n40%"   "None\n20%"