我有单行EditText
,我对内容的大小与EditText
的大小感兴趣,以查看内容是否符合EditText
的大小或不。我怎么能这样做?
由于
答案 0 :(得分:2)
我不确定你在这里问的是什么:
您可以像这样测量文字宽度:(取自Alan Jay Weiner: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
}
});