2013-04-22 181 views

回答

2

我真的不知道你問這裏是什麼:

可以測量的文本的寬度是這樣的:(從阿蘭·傑伊·韋納在這裏拍攝:Auto Scale TextView Text to Fit within Bounds

// Set the text size of the text paint object and use a static layout to render text off screen before measuring 
private int getTextWidth(CharSequence source, TextPaint paint, int width, float textSize) { 
    // Update the text paint object 
    paint.setTextSize(textSize); 
    // Draw using a static layout 
    StaticLayout layout = new StaticLayout(source, paint, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, true); 
    layout.draw(sTextResizeCanvas); 
    return layout.getWidth(); 
} 

,你可以用這樣的觀察者測量文本:

yourEditText.addTextChangedListener(new TextWatcher() { 

      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
      } 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       //measure your stuff here 
      } 
      @Override 
      public void afterTextChanged(Editable s) { 
       // TODO Auto-generated method stub 

      } 
     });