您的位置:首页 > 移动开发 > Android开发

Android中获取字符串长度、宽度(所占像素宽度)的方法

2017-07-18 18:20 337 查看
在Android开发中的自定义view中,有时需要根据字符串的宽度确定其所处的位置,确定字符串宽度的方法如下:

方法1:

Paint pFont = new Paint();

Rect rect = new Rect();

//返回包围整个字符串的最小的一个Rect区域

pFont.getTextBounds(str, 0, 1, rect);

strwid = rect.width();

strhei = rect.height();

方法2:

//直接返回参数字符串所占用的宽度

TextPaint textPaint = new TextPaint();

strwid =
textPaint.measureText(str);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android