如何计算像素串字体宽度?宽度、像素、字体

2023-09-04 23:56:21 作者:烟波人长安

我要计算在机器人像素字符串字体宽度。我有字符串数组,我追加所有这些数组元素形成一个字符串。所以我要在两个连续的阵列之间element.i我把这个最后的字符串到TextView中设置固定的间距。最后,我加入这个TextView的为tablerow。总之我想与标题和值查看表结构。我看过很多帖子在这里,但没有得到解决方案这一点。

I want to calculate string font width in pixels in android. I am having string array and I am appending all these array elements to form one string. so I have to set fixed spacing in between two consecutive array element.i am placing this final string into textview. Finally I am adding this textview to tablerow. In short i want to view table structure with title and values. I have seen many post over here but not getting solution for this.

在此先感谢... !!!

Thanks in advance...!!!

推荐答案

看起来像有一个 measureText 方法可在Paint.我还发现了一个example:

Looks like there is a measureText method available on Paint. I also found an example:

mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(5);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setTextSize(64);
mPaint.setTypeface(Typeface.create(Typeface.SERIF, Typeface.ITALIC));
// ...
float w = mPaint.measureText(text, 0, text.length());