我想使用 TreeObserver 方法获取 View 的高度:
int v1_h;
v1.getViewTreeObserver().addOnGlobalLayoutListener(newViewTreeObserver.OnGlobalLayoutListener() {
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
//now we can retrieve the width and height
v1_h = v1.getHeight();
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
v1.getViewTreeObserver().removeOnGlobalLayoutListener(this);
else
v1.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
//Do something with v1_h later on
我想将高度存储在一个变量中并在以后使用。问题是它需要声明为 final 才能从内部类中访问。所以我把它定下来了,但现在它说它不能被分配。有想法该怎么解决这个吗?
最佳答案
最简单的方法是将变量声明为类成员而不是在堆栈上声明的变量。如果您将 v1_h 设为最终版本,您将无法真正按照预期的方式使用它。
关于java - 获取 View 的高度 - 变量需要声明为 final,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36488361/