字符串宽度可以这样:
Paint paint = new Paint();
float strWidth = paint.measureText(String);
paint.setTextSize(tv.getTextSize());
二.
private final Paint pFont = new Paint();
Rect rect = new Rect();
pFont.getTextBounds("小", 0, 1, rect);
Log.v(TAG, "height:"+rect.height()+"width:"+rect.width());
Paint paint = new Paint();
float strWidth = paint.measureText(String);
paint.setTextSize(tv.getTextSize());
二.
private final Paint pFont = new Paint();
Rect rect = new Rect();
pFont.getTextBounds("小", 0, 1, rect);
Log.v(TAG, "height:"+rect.height()+"width:"+rect.width());
解决方案 »
- 有人了解过屏幕方向改变原理么,在源码什么地方处理根据外界环境控制着系统屏幕的方向变化?
- anroid 通知栏语言切换问题
- 为什么Android中的R文件不随着其它文件的更改而更新数据
- 【200分】请问这些网上商城之类的软件用的什么框架?
- android从数据库(MSDB)读取图片并显示
- ListView 大量数据问题
- 有哪位android大神弄过像wps那样浏览word文档
- java.lang.IllegalArgumentException: pointerIndex out of range
- 求一个页面三个图片,点击图片后全屏显示图片,再点击则还原。急求!!!
- adb 删除 系统预装文件的问题
- 关于Activity切换!求教!
- PackageManager的Features设置问题
mTextPaint.setColor(Color.WHITE);
// Define the string.
String displayText = “Hello World!”;
// Measure the width of the text string.
float textWidth = mTextPaint.measureText(displayText);
大家都倾向于上面的方法,但是我测试的时候为什么会比实际小呢,大家测了没有
Rect rect = new Rect();
pFont.getTextBounds("小", 0, 1, rect);
Log.v(TAG, "height:"+rect.height()+"width:"+rect.width());
我用的是这个,比实际小,倒是没测。
但是我真的没有找到比这个更好的方法了。
那个getsize()肯定是不能用的,大小绝对不对。
mPaint.setTextSize(16);
float FontSpace = mPaint.getFontSpacing();
return text.length()*FontSpace;
最后的结果 用字符串长度 × 字体宽度。得到字符长度最接近实际长度,但是当出现英文或者是标点符号时,长度就会缩小