gpt4 book ai didi

android - 为什么 MeasuredHeight 始终为 0,如何获取该值?

转载 作者:太空狗 更新时间:2023-10-29 16:40:22 33 4
gpt4 key购买 nike

我正在尝试应用动画来折叠 LinearLayout。此动画依赖于 LinearLayout 的 MeasuredHeight 属性,其值大于 0,但该值始终为 0。

这是我正在使用的代码:

    public void Collapse()
{
var v = FindViewById<LinearLayout>(Resource.Id.layoutBranding);
int initialHeight = v.MeasuredHeight; // always 0!!

var a = new MyAnimation(v, initialHeight);

a.Duration = (int)(initialHeight / v.Context.Resources.DisplayMetrics.Density);
v.StartAnimation(a);
}

MyAnimation 定义为:

公共(public)类 MyAnimation :动画 { 私有(private)只读 View _view; 私有(private)只读 int _initalHeight;

    public MyAnimation(View view, int initalHeight)
{
_view = view;
_initalHeight = initalHeight;
}

protected override void ApplyTransformation(float interpolatedTime, Transformation t)
{
if (interpolatedTime == 1)
{
_view.Visibility = ViewStates.Gone;
}
else
{
_view.LayoutParameters.Height = _initalHeight - (int) (_initalHeight*interpolatedTime);
_view.RequestLayout();
}
}

public override bool WillChangeBounds()
{
return true;
}
}

最佳答案

试试这个:

v.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);


int initialHeight = v.MeasuredHeight;

关于android - 为什么 MeasuredHeight 始终为 0,如何获取该值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18739110/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com