使用getViewTreeObserver()时,View.getHeight()返回0

时间:2014-12-10 02:39:51

标签: android height

我想在绘制之前获取textView高度,所以我使用其他问题的getViewTreeObserver代码:

private TextView mTextView;
private int mTextViewHeight = 0;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) { 
mRootView = inflater.inflate(R.layout.fragment, container, false);

mTextView = (TextView) mRootView.findViewById(R.id.text);

mTextView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                    @Override
                    public void onGlobalLayout() {
                        mTextViewHeight = mTextView.getHeight(); // Tried with getMeasured() too

                        if (Utils.hasJellyBean()) {
                            mTextView.getViewTreeObserver()
                                    .removeOnGlobalLayoutListener(this);
                        } else {
                            mTextView.getViewTreeObserver()
                                    .removeGlobalOnLayoutListener(this);
                        }
                    }
                });

使用此代码,我总是得到mTextViewHeight = 0.我已经尝试过debuggin,并且查看OnGlobalLayoutListener的入口点,线程永远不会访问" mTextViewHeight = mTextView.getHeight();"线。

谢谢!

修改

答案很棒,但我的错误是,当我想在代码中使用mTextViewHeight时,此代码已在Listener上输入之前执行,因此我得到的值是初始值。要解决这个问题,我必须在监听器中放入我想要与textView高度一起使用的代码。

在固定之前:

private TextView mTextView;
    private int mTextViewHeight = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) { 
    mRootView = inflater.inflate(R.layout.fragment, container, false);

    mTextView = (TextView) mRootView.findViewById(R.id.text);

    mTextView.getViewTreeObserver().addOnGlobalLayoutListener(
                    new ViewTreeObserver.OnGlobalLayoutListener() {
                        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                        @Override
                        public void onGlobalLayout() {
                            mTextViewHeight = mTextView.getHeight(); // Tried with getMeasured() too

                            if (Utils.hasJellyBean()) {
                                mTextView.getViewTreeObserver()
                                        .removeOnGlobalLayoutListener(this);
                            } else {
                                mTextView.getViewTreeObserver()
                                        .removeGlobalOnLayoutListener(this);
                            }
                        }
                    });

    if(mTextViewHeight > maxHeightAllowed) {
        mTextViewHeight /= 2;
    } else {
        String text = new String("");
        for(int i = 0; i < mTextViewHeight.lenght(); i++)
        text = text + getNextWord[i];
    }
}

固定后

private TextView mTextView;
private int mTextViewHeight = 0;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) { 
mRootView = inflater.inflate(R.layout.fragment, container, false);

mTextView = (TextView) mRootView.findViewById(R.id.text);

mTextView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                    @Override
                    public void onGlobalLayout() {
                        mTextViewHeight = mTextView.getHeight(); // Tried with getMeasured() too
                        if(mTextViewHeight > maxHeightAllowed) {
                            mTextViewHeight /= 2;
                        } else {
                            String text = new String("");
                            for(int i = 0; i < mTextViewHeight.lenght(); i++)
                            text = text + getNextWord[i];
                        }

                        if (Utils.hasJellyBean()) {
                            mTextView.getViewTreeObserver()
                                    .removeOnGlobalLayoutListener(this);
                        } else {
                            mTextView.getViewTreeObserver()
                                    .removeGlobalOnLayoutListener(this);
                        }
                    }
                });

}

1 个答案:

答案 0 :(得分:0)

我认为你应该使用onPreDraw函数:

    ViewTreeObserver greenObserver = mTextViewHeight.getViewTreeObserver();
    greenObserver.addOnPreDrawListener(new OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {
            mTextViewHeight.getViewTreeObserver().removeOnPreDrawListener(this);
            System.out.println("The height is " + mTextViewHeight.getHeight());
            return true;
        }
    });