【发布时间】:2015-09-02 09:03:24
【问题描述】:
这是我返回视图高度的代码:
int height = 0;
public int getViewHeight(final View v) {
v.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
v.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
} else {
v.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
}
height = v.getMeasuredHeight();
//
Log.v(" height", v.getMeasuredHeight() + "");
}
});
Log.v(" return height", v.getMeasuredHeight() + "");
return height;
}
首先它返回0。然后高度值改变。 任何人都可以让我知道如何在 onGlobalLayout 中测量时返回视图高度。 Return 语句在被覆盖的方法中不起作用。
或
有没有其他方法可以通过传递 View 实例来制作一个返回视图高度的静态方法。
【问题讨论】: