ImageView高度总是在android中返回零

时间:2015-09-26 12:56:24

标签: android

我知道这有点重复,但其他问题的答案并没有帮助我。我的ImageView高度始终为零。这是我试过的:

dart.getViewTreeObserver().addOnPreDrawListener(
    new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {
        int finalHeight = dart.getMeasuredHeight();
        int finalWidth = dart.getMeasuredWidth();
        Toast.makeText(MainActivity.this," "+finalHeight,Toast.LENGTH_SHORT).show();
        return true;
    }
});

另外 -

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    height = dart.getHeight();
    width = dart.getWidth();
    Toast.makeText(MainActivity.this," "+height,Toast.LENGTH_SHORT).show();
}

我还在dart.getHeight()内尝试了onCreate,但我知道它无法正常工作。我该怎么办?

1 个答案:

答案 0 :(得分:1)

ViewTreeObserver添加到dart.getViewTreeObserver().removeOnPreDrawListener(this);并在onPreDraw()方法中致电ViewTreeObserver vto = iv.getViewTreeObserver(); vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { public boolean onPreDraw() { dart.getViewTreeObserver().removeOnPreDrawListener(this); finalHeight = dart.getMeasuredHeight(); finalWidth = dart.getMeasuredWidth(); Toast.makeText(MainActivity.this," "+finalHeight,Toast.LENGTH_SHORT).show(); return true; } });

{{1}}