对于动画,我需要知道视图的高度。问题是,除非绘制视图,否则 getHeight() 方法总是返回 0。
那么有没有办法不用画图就能得到高度呢?
在本例中,View 是 LinearLayout。
编辑:
我尝试改编扩展动画https://github.com/Udinic/SmallExamples/blob/master/ExpandAnimationExample/src/com/udinic/expand_animation_example/ExpandAnimation.java
我想用它来扩展列表项的更多信息。
我无法通过 xml 达到相同的效果。
目前,只有在绘制之前知道布局尺寸时,动画才会起作用。
听起来您想获取高度但在视图可见之前隐藏视图。
首先将视图中的可见性设置为可见或不可见(只是为了创建高度)。别担心,我们将在代码中将其更改为不可见/消失,如下所示:
private int mHeight = 0;
private View mView;
class...
// onCreate or onResume or onStart ...
mView = findViewByID(R.id.someID);
mView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener(){
@Override
public void onGlobalLayout() {
// gets called after layout has been done but before display
// so we can get the height then hide the view
mHeight = mView.getHeight(); // Ahaha! Gotcha
mView.getViewTreeObserver().removeGlobalOnLayoutListener( this );
mView.setVisibility( View.GONE );
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)