0

我有一个单行EditText,我对内容的大小与 的大小感兴趣EditText,看看内容是否适合 的大小EditText。我怎样才能做到这一点?

谢谢

4

1 回答 1

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

            }
        });
于 2013-04-22T14:47:16.057 回答