Textview的getMeasuredHeight()返回错误的值

时间:2014-05-26 06:06:34

标签: android

我在TextView的父布局的onMeasure()返回后立即调用所有提到的方法,如下所示:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int mh = mTextView.getMeasuredHeight();
    int h = mTextView.getLineCount() * mTextView.getLineHeight();
}

mh错误且比h短,有谁知道原因,谢谢?

1 个答案:

答案 0 :(得分:1)

// Try this way,hope this will help you to solve your problem...

  mTextView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    int mh = mTextView.getMeasuredHeight();
                    int h = mTextView.getLineCount() * mTextView.getLineHeight();
                }
  });