Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么测量高度始终为0,如何获取该值?_Android_Xamarin.android_Xamarin - Fatal编程技术网

Android 为什么测量高度始终为0,如何获取该值?

Android 为什么测量高度始终为0,如何获取该值?,android,xamarin.android,xamarin,Android,Xamarin.android,Xamarin,我正在尝试将动画应用于折叠线性布局。此动画取决于线性布局的MeasuredHeight特性,该特性的值大于0,但该值始终为0 这是我正在使用的代码: public void Collapse() { var v = FindViewById<LinearLayout>(Resource.Id.layoutBranding); int initialHeight = v.MeasuredHeight; // always 0!!

我正在尝试将动画应用于折叠线性布局。此动画取决于线性布局的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);
    }

在视图实际布局之前(渲染之前),它的高度和宽度始终为0


如果您在测量上测量高度,或者仅在布局上测量高度(我认为),那么它应该具有非零高度-

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus)
        {
            // Get the height and the width of the view 
        }
    }
试试这个:

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


int initialHeight = v.MeasuredHeight; 

您正在调用oncreate中的崩溃吗?不,它在OnResume中。请尝试将其放在onWindowFocusChanged中。你是说
AddOnLayoutChangeListener
?不,我建议扩展OP正在使用的视图,然后重写其中一个方法+1。所有其他答案都依赖于视图是自定义视图(其中必须扩展LinearLayout)。这应该在运行时测量您的任何可见视图,自定义视图或其他视图。太棒了!你也可以这样做<代码>视图.measure(视图.MeasureSpec.UNSPECIFIED,视图.MeasureSpec.UNSPECIFIED)
int initialHeight=view.getMeasuredHeight()
v.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);


int initialHeight = v.MeasuredHeight;