Android 中如何得到字符串的像素宽度。?
条件是:在不同字体,和不同大小的情况下都可以
字符串宽度可以这样:
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 mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(Color.WHITE);
// Define the string.
String displayText = “Hello World!”;
// Measure the width of the text string.
float textWidth = mTextPaint.measureText(displayText);


Paint mPaint = new Paint();
mPaint.setTextSize(16);
float FontSpace = mPaint.getFontSpacing();
return text.length()*FontSpace;
最后的结果 用字符串长度 × 字体宽度。得到字符长度最接近实际长度,但是当出现英文或者是标点符号时,长度就会缩小

更多相关文章

  1. android获取屏幕的宽度和高度
  2. Android handler.obtainMessage()传字符串用法
  3. Android DrawText字符串的绘制
  4. android获取手机屏幕的宽度及高度
  5. Android WebView 图片自适应屏幕宽度
  6. Android动态改变高度以及计算长度的EditText
  7. Android xliff和字符串资源[Android开发网]
  8. Android studio 提取文件指定字符串

随机推荐

  1. 异步加载注意的事项
  2. Android(安卓)从一个应用打开另一个应用
  3. Android踩坑日记:FloatingActionButton的
  4. Android——Fragment A点击一个按钮跳转
  5. android 6.0(api 23) SDK,不再提供org.apa
  6. android事件分发(三)重要的函数requestDisa
  7. Android(安卓)消息推送通知指南
  8. Android(安卓)文件操作
  9. 如何隐藏APP名字
  10. Android(安卓)8.0 高通代码预制apk可卸载