我有类似这样的问题:Update layout with the animation
基本上:我有一个带有edittext,按钮和列表的垂直LinearLayout视图 . 我想在按下按钮后隐藏exittext,为列表腾出更多空间(按钮会上升) . 在第二次按下edittext应该再次可见 . Edittext和按钮具有“wrap_content”高度 .
我想用动画隐藏和显示edittext .
我成功地通过重载动画的applyTransformation来动画隐藏:
final float edittextheight= edittext.getHeight();
[....]
@Override
protected void applyTransformation(float interpolatedTime,
Transformation t)
{
super.applyTransformation(interpolatedTime, t);
android.view.ViewGroup.LayoutParams lp = edittext.getLayoutParams();
lp.height = (int)(edittextheight*(1.0-interpolatedTime));
edittext.setLayoutParams(lp);
}
问题:
我不知道如何计算动画显示的高度 - edittext.getHeight();隐藏窗口小部件时返回0,并且在布局定义中我使用“wrap_content” .
救命?
2 回答
在活动完成绘制之前,您无法获得edittext的高度 . 因此,您可以通过在编辑文本上添加onpredrawListner来获取它 .
现在你得到了高度,应用你想要的任何动画 .
你可以缓存EditText的高度吗?换句话说,当你隐藏它时,在int中保存它的高度(使用getHeight()),即使窗口小部件本身不再可见,你也可以稍后读取它 .
但是,我怀疑你也遇到了类似的问题,如question所述 . 基本上它是's that you can' t得到一个View的高度,直到onCreate,onStart和onPause都已完成,因为在此之前活动没有't finished drawing itself on the screen. A good way to know that the layout process is finished before you try to get its height is simply to get the height in your button ClickListener; you simply can' t点击按钮,直到布局完成,所以它将完成计算高度 .