我正在尝试应用动画来折叠线性布局。此动画取决于值大于0的LinearLayout的MeasuredHeight属性,但该值始终为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定义为:
公共类myanimation:动画
{
专用只读视图;
private readonly 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/

10-12 22:27