字符串宽度可以这样:
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()); 

解决方案 »

  1.   

    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);   
     
      

  2.   

    mTextPaint.measureText(displayText);   
    大家都倾向于上面的方法,但是我测试的时候为什么会比实际小呢,大家测了没有
      

  3.   

    private final Paint pFont = new Paint();
    Rect rect = new Rect();
    pFont.getTextBounds("小", 0, 1, rect);
    Log.v(TAG, "height:"+rect.height()+"width:"+rect.width());  
    我用的是这个,比实际小,倒是没测。
    但是我真的没有找到比这个更好的方法了。
    那个getsize()肯定是不能用的,大小绝对不对。
      

  4.   

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

  5.   

    某些字符的 rect.left > 0