我正在尝试应用动画来折叠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定义为:
公共类MyAnimation:动画 { private readonly查看_view; 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;
}
}
答案 0 :(得分:4)
试试这个:
v.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int initialHeight = v.MeasuredHeight;
答案 1 :(得分:1)
尝试在 -
中测量视图的高度和宽度 @Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus)
{
// Get the height and the width of the view
}
}
答案 2 :(得分:0)
直到View实际布局(在渲染之前),它的高度和宽度始终为0
如果您在onMeasure
或onLayout
(我认为)中测量高度,那么它应该具有非零高度